how to load local file in InAppWebView in flutter?


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


Yes you can do that. Here are the steps:

See the example here, see how it uses HTML strings such as kNavigationExamplePage:

const String kNavigationExamplePage = '''
<!DOCTYPE html><html>
<head><title>Navigation Delegate Example</title></head>
The navigation delegate is set to block navigation to the youtube website.
<ul><a href=""></a></ul>
<ul><a href=""></a></ul>

  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

If you are using flutter_inappwebview, it seems it even has a function that uses your asset files directly:

Answered By – Gazihan Alankus

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *