Background
Im using webview_flutter 3.0.0
in order to render a complex HTML string that I'm creating in real time. So its not content loaded from the web. I'm doing this as follows:
final Completer<WebViewController> _controller =
Completer<WebViewController>();
late WebViewController _con;
var finalThreadHTML;
_loadHTML() async {
finalThreadHTML = createComplexHTMLStringInRealTime();
}
_con.loadUrl(Uri.dataFromString(
setThreadHTML(
finalThreadHTML
),
mimeType: 'text/html',
encoding: Encoding.getByName('utf-8')
).toString());
}
and then in my UI
WebView(
initialUrl: '',
javascriptMode: JavascriptMode.unrestricted,
javascriptChannels: <JavascriptChannel>[
JavascriptChannel(
name: 'MessageInvoker',
onMessageReceived: (s) {
}),
].toSet(),
onWebViewCreated: (WebViewController webViewController) {
_con = webViewController;
_loadHTML();
},
onProgress: (int progress) {
print("WebView is loading (progress : $progress%)");
},
navigationDelegate: (NavigationRequest request) {
if (request.url.startsWith('https://www.youtube.com/')) {
print('blocking navigation to $request}');
return NavigationDecision.prevent;
}
print('allowing navigation to $request');
return NavigationDecision.navigate;
},
onPageStarted: (String url) {
print('Page started loading: $url');
},
onPageFinished: (String url) {
print('Page finished loading: $url');
},
gestureNavigationEnabled: true,
)
My realtime HTML string (createComplexHTMLStringInRealTime() )looks something like this:
String createComplexHTMLStringInRealTime(){
return ('''
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body style="height:100vh;">
Testing some stuff
<img src="/images/blankAvatar.jpg" />
Testing more stuff
</body>
</html>
''');
}
My question
In my formed HTML string I want to include some images, which are local asset files as defined in pubspec.yaml
So I tried doing this:
<img src="/images/blankAvatar.jpg" />
but that did not load anything, then I tried
<img src="file:///images/blankAvatar.jpg" />
Still nothing.
Is there anyway to load local files inline with my generated text to load a local file into the webview?