'PostMessage from flutter to inappwebview

There are many answers to this regarding the flutter_webview package but not a single one for flutter_inappwebview which we are using for a few reasons.

So is there any way to fire up a method from anywhere inside the flutter widget tree that posts/sends anything to the inappwebview for which I can listen there with JS to do something (like a console.log with the received message)?

I also opened a github issue with a little more detail and a code snippet for this although I'm not sure how frequently the dev is looking for it (he has many issues there) so I figured asking the stackoverflow community makes sense. Here's the github issue for convenience: https://github.com/pichillilorenzo/flutter_inappwebview/issues/1037



Solution 1:[1]

you can trigger javascript function from flutter by following these steps

  1. Need to execute evaluate javascript function from flutter as below

    _webViewController.evaluateJavascript(source: """
     var event = new CustomEvent("demoJavaScriptListener", {detail: {});
    window.dispatchEvent(event); """);
    
  2. Now in Javascript side you need write a listener as below

    window.addEventListener("demoJavaScriptListener", (event) => {
        console.log("My custom Event");
        var map= JSON.parse(JSON.stringify(event.detail));
        console.log("map",map);
    });
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Neela Jashwanth