Search code examples
fluttereventsiframe

Calling postMessage on iframeElement in flutter web


I have a flutter web application. I need to call postMessage to be handled by the page opened in an iframe element using

 ui.platformViewRegistry.registerViewFactory(
            'hello-world-html',
            (int viewId) => html.IFrameElement()
              ..width = '100%'
              ..height = '100%'
              ..src = widget.url
              ..style.border = 'none'
              );

Is there a way to do this?


Solution

  • Technically, PostMessage sends to the main document's window, not to the iframe's.

    Specify the iframe's window object in Javascript:

    document.getElementById('hello-world-html').contentWindow.postMessage(.......)
    

    or in Dart you can do:

    import 'dart:html' as html;
    
    // Your code
    
    html.document.getElementById.getElementById('hello-world-html').contentWindow.postMessage(.......)