我们正在开发 flutter 应用程序以支持离线功能。这需要存储文件(pdf、二进制数据等)。我们为此选择应用程序文档目录。然后我们还需要将本地路径传递给 API 命令。
使用 inappwebview 包创建我们的 http 服务器并读取存储在 app doc 目录中的文件后,flutter 将从资产文件夹中读取路径(它总是将扩展名“assets/”添加到输入路径)。
return InAppWebView(
initialSettings: InAppWebViewSettings(
webViewAssetLoader: WebViewAssetLoader(pathHandlers: [
// InternalStoragePathHandler(path: "", directory: pathdir)
// AssetsPathHandler(
// path: "/data/user/0/com.example.minisitewalkapp/app_flutter/")
]),
allowFileAccess: true), //doesnt solve the issue!!
onConsoleMessage: (controller, consoleMessage) {
print(consoleMessage.message);
},
initialUrlRequest: URLRequest(
url: WebUri.uri(
Uri.parse("http://localhost:8080/assets/wwwroot/index.html"))),
onWebViewCreated: (controller) {
// controller.loadUrl(
// urlRequest: URLRequest(
// url: Uri.parse(
// "file://data/user/0/com.example.minisitewalkapp/app_flutter/Floor Plan/Unit Floor Plan1.pdf")));
},
);
是否有解决方法或官方支持从应用程序文档目录中引用文件的方法?
问候, 米格尔 G
我们甚至尝试使用新的 InAppWebViewSettings 选项,但没有成功从资产文件夹中删除讲座:
initialSettings: InAppWebViewSettings(
webViewAssetLoader: WebViewAssetLoader(pathHandlers: [
// InternalStoragePathHandler(path: "", directory: pathdir)
// AssetsPathHandler(
// path: "/data/user/0/com.example.minisitewalkapp/app_flutter/")
]),
allowFileAccess: true),
事实证明,android 的一个解决方法是实现一个 customPathHandler。查看此链接:https://inappwebview.dev/docs/webview/webview-asset-loader 我们可以拦截 flutter 资产请求,并返回来自应用程序文档目录的字节。
对于ios,需要设置url schema并实现onLoadResourceWithCustomScheme命令