首页 > 解决方案 > Flutter 无法使用 webview 显示本地文件

问题描述

我正在尝试使用 webview 显示本地文件(如果有其他选项,欢迎使用,但我试图显示的不仅仅是 pdf。我想显示 pdf、xlsx、docx)。

我正在提供带有 url 的 url file://...,首先它给了我使用flutter_webview_pluginwebview_flutter插件的访问被拒绝错误。然后我编辑了两个插件的源代码,并setAllowFileAccessFromFileURLs根据参考配置为 true,但这次它只向我显示白色空白页。并且控制台中有奇怪的警告(甚至在我编辑源代码之前就显示了这个警告。)

警告:

W/m.packagename(31636): Couldn't lock the profile file /data/user/0/packagename/app_tbs_64/core_share/oat/tbs_sdk_extension_dex.jar.cur.prof: Failed to open file '/data/user/0/packagename/app_tbs_64/core_share/oat/tbs_sdk_extension_dex.jar.cur.prof': No such file or directory W/m.gekapp.gekap(31636): Could not forcefully load profile /data/user/0/packagename/app_tbs_64/core_share/oat/tbs_sdk_extension_dex.jar.cur.prof

我在手机上使用 Android 11,我认为这可能是问题所在。(在iOS上没有问题我可以使用显示文件file://...

flutter doctor --verbose

[✓] Flutter (Channel stable, 2.2.0, on macOS 11.3 20E232 darwin-arm, locale en-TR)
    • Flutter version 2.2.0 at /Users/burakcabadan/development/flutter
    • Framework revision b22742018b (4 weeks ago), 2021-05-14 19:12:57 -0700
    • Engine revision a9d88a4d18
    • Dart version 2.13.0

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/burakcabadan/Library/Android/sdk
    • Platform android-30, build-tools 30.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.5, Build version 12E262
    • CocoaPods version 1.10.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)

[✓] IntelliJ IDEA Ultimate Edition (version 2021.1)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin version 55.1.5
    • Dart plugin version 211.7233

[✓] VS Code (version 1.56.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.23.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 11.3 20E232 darwin-arm
    • Chrome (web)    • chrome • web-javascript • Google Chrome 91.0.4472.77

• No issues found!

标签: androidflutterwebviewflutterwebviewpluginwebview-flutter

解决方案


推荐阅读