flutterflutter-inappwebview

How to load HTML from String in InAppWebView?


I'm sometimes receiving HTML as a response from network requests. I've encountered a scenario where I need to display HTML content within the InAppWebView. Is there any short way to do that.

I tried to find answer on the Internet, but couldn't.


Solution

  • Turns out we can do it through initialData parameter:

    InAppWebView(
        initialData: InAppWebViewInitialData(data: htmlExample),
    )
    
    const String htmlExample = '''
    <!DOCTYPE html><html>
    <head><title>HTML wxample</title></head>
    <body>
    <p>
    HTML example
    </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>
    ''';
    

    Also with InAppWebViewController:

    webViewController.loadData(htmlExample);