I want to load files from internal storage in In App WebView in flutter so that I can load that file in WebView.
Asked
Active
Viewed 4,928 times
2 Answers
4
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

Gazihan Alankus
- 11,256
- 7
- 46
- 57
-
can we do that, in this plugin - InAppWebView? – Saikiran Mar 02 '22 at 07:29
-
Yes, please see my edit at the bottom of the answer. https://pub.dev/documentation/flutter_inappwebview/latest/flutter_inappwebview/InAppWebViewController/loadFile.html – Gazihan Alankus Mar 02 '22 at 07:34
-
yes it's right, but I want to load it from internal storage instead of assets, can we do that? – Saikiran Mar 02 '22 at 07:45
-
yes https://pub.dev/documentation/flutter_inappwebview/latest/flutter_inappwebview/InAppWebViewController/loadData.html – Gazihan Alankus Mar 02 '22 at 07:47
-
If this answer helped you please consider accepting it. – Gazihan Alankus Mar 02 '22 at 07:55
-
yes it's working thanks. – Saikiran Mar 03 '22 at 08:52
-
great, can you mark the answer as accepted using the tick icon next to it? – Gazihan Alankus Mar 03 '22 at 09:00
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.

mirkancal
- 4,762
- 7
- 37
- 75