4

I want to load files from internal storage in In App WebView in flutter so that I can load that file in WebView.

3 Answers 3

5

Yes you can do that. Here are the steps:

See the example here, see how it uses HTML strings such as kNavigationExamplePage: https://pub.dev/packages/webview_flutter/example

const String kNavigationExamplePage = '''
<!DOCTYPE html><html>
<head><title>Navigation Delegate Example</title></head>
<body>
<p>
The navigation delegate is set to block navigation to the youtube website.
</p>
<ul>
<ul><a href="https://www.youtube.com/">https://www.youtube.com/</a></ul>
<ul><a href="https://www.google.com/">https://www.google.com/</a></ul>
</ul>
</body>
</html>
''';

  Future<void> _onNavigationDelegateExample(
      WebViewController controller, BuildContext context) async {
    final String contentBase64 =
        base64Encode(const Utf8Encoder().convert(kNavigationExamplePage));
    await controller.loadUrl('data:text/html;base64,$contentBase64');
  }

What you need to do is to read this string from a File instead. This answer provides detailed steps on how to do that. Instead of a text file, you will read an HTML file. Later you will use it instead of the kNavigationExamplePage string.

Flutter - Read text file from assets

Edit: If you are using flutter_inappwebview, it seems it even has a function that uses your asset files directly: https://pub.dev/documentation/flutter_inappwebview/latest/flutter_inappwebview/InAppWebViewController/loadFile.html

Sign up to request clarification or add additional context in comments.

8 Comments

can we do that, in this plugin - InAppWebView?
Yes, please see my edit at the bottom of the answer. pub.dev/documentation/flutter_inappwebview/latest/…
yes it's right, but I want to load it from internal storage instead of assets, can we do that?
If this answer helped you please consider accepting it.
|
0
 WebView(
          initialUrl: htmlToURI(code),
    ...
),


String htmlToURI(String code) {
  return Uri.dataFromString(code,
          mimeType: 'text/html', encoding: Encoding.getByName('utf-8'))
      .toString();
}

That was working in my case.

Comments

0

Here a simple example which loads a html file from flutter assets:

@override
Widget build(BuildContext context) {
   return InAppWebView(initialFile: "assets/html/index.html");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.