Search code examples
iframedartpostmessage

How to receive data from an Iframe in Dart?


I know that the only way to communicate with an iframe is using the postMessage method as follow:

iframe.contentWindow.postMessage("my message", window.location.href);

but I would like to know how to receive data from the iframe after any change on it.


Solution

  • In the receiver window, when you receive the message, you get the sender passed with the message and you can send back to this sender.

    window.onMessage.listen((e) {
      e.source.postMessage({'somedata': 'xxx'}, '*');
    });
    

    See also