I want to render a local HTML file stored in my phone memory in webview using flutter and dart.
相关问题
- What means in Dart static type and why it differs
- Android Exclude Some Camera Intent
- Flutter : Prepare list data from http request
- How to schedule an alarm on specific time in Flutt
- MappedListIterable is not a SubType
相关文章
- Observatory server failed to start - Fails to crea
- Flutter error retrieving device properties for ro.
- Adding Shadows at the bottom of a container in flu
- Flutter. Check if a file exists before loading it
- Flutter - http.get fails on macos build target: Co
- Receive share file intents with Flutter
- Do stateless widgets dispose on their own?
- How to clean your build with Flutter RP2 in Androi
Here is an answer with a little more detail. I am using the webview_flutter plugin from the Flutter Team.
Steps
Add the dependency to pubspec.yaml:
Put an html file in the
assets
folder (see this). I'll call ithelp.html
.Get the html string in code and add it to the webview.
Notes:
io.flutter.embedded_views_preview
astrue
in the Info.plist file. Check the docs for any update on this requirement.See also
You can use my plugin flutter_inappwebview, which has a lot of events, methods, and options compared to other plugins!
To load an html file from your assets folder, you need to declare it in the
pubspec.yaml
file before use it (see more here).Example of a
pubspec.yaml
file:After that, you can simply use the
initialFile
parameter of theInAppWebView
widget to loadindex.html
into the WebView:unzip the apk package,I found the reason: path is wrong;
For Android:
"assets/test.html" == "file:///android_asset/flutter_assets/assets/test.html"
so,just like this:
you can load "assets/test.html".
You can pass a data URI
or you can launch a web server inside Flutter and pass an URL that points to the IP/port the server serves the file from.
See also the discussion in https://github.com/fluttercommunity/flutter_webview_plugin/issues/23
See https://flutter.io/docs/development/ui/assets-and-images#loading-text-assets about how to load a string from assets.
See https://flutter.io/docs/cookbook/persistence/reading-writing-files for how to read other files.
I have the same problem; this is how I solved it.
Add webview_flutter to your project dependencies:
webview_flutter: 0.3.14+1
Create a WebViewController inside your screen/stateful widget
WebViewController _controller;
Implement the WebView and assign a value to the _controller using the onWebViewCreated property. Load the HTML file.
@Suragch, your code doesn't work as you posted it, it says localUrl was called on null. _loadHtmlFromAssets needs to be called after assigning the controller :
Then it works fine :)