getHtml method
Gets the content html of the page. It first tries to get the content through javascript. If this doesn't work, it tries to get the content reading the file:
- checking if it is an asset (
file:///
) or - downloading it using an
HttpClient
through the WebView's current url.
Returns null
if it was unable to get it.
Officially Supported Platforms/Implementations:
- Android native WebView
- iOS
- MacOS
- Web
Implementation
@override
Future<String?> getHtml() async {
String? html;
InAppWebViewSettings? settings = await getSettings();
if (settings != null && settings.javaScriptEnabled == true) {
html = await evaluateJavascript(
source: "window.document.getElementsByTagName('html')[0].outerHTML;");
if (html != null && html.isNotEmpty) return html;
}
var webviewUrl = await getUrl();
if (webviewUrl == null) {
return html;
}
if (webviewUrl.isScheme("file")) {
var assetPathSplit = webviewUrl.toString().split("/flutter_assets/");
var assetPath = assetPathSplit[assetPathSplit.length - 1];
try {
var bytes = await rootBundle.load(assetPath);
html = utf8.decode(bytes.buffer.asUint8List());
} catch (e) {}
} else {
try {
HttpClient client = HttpClient();
var htmlRequest = await client.getUrl(webviewUrl);
html =
await (await htmlRequest.close()).transform(Utf8Decoder()).join();
} catch (e) {
developer.log(e.toString(), name: this.runtimeType.toString());
}
}
return html;
}