-
Notifications
You must be signed in to change notification settings - Fork 30.2k
postMessage Web doesn't work as postMessage Html #174629
Description
Steps to reproduce
- Crate a new flutter project i.e. "flutter create test_postmessage".
- Add in pubspec.yaml the dependency "web: ^1.1.1".
- In main.dart, at the top add these lines:
import 'dart:html' as html;
import 'dart:js_interop';
import 'package:web/web.dart' as web;
- Then go down to class _MyHomePageState and after "int _counter = 0;" add these lines:
void _sendMessageByHtml() {
html.window.parent?.postMessage({
'func': 'parentFunc',
'message': 'Sent by iframe (Html)'
}, "*");
}
void _sendMessageByWeb() {
web.window.parent?.postMessage({
'func': 'parentFunc',
'message': 'Sent by iframe (Web)'
}.toJSBox, "*".toJS);
}
-
Then after "void _incrementCounter() {" add these lines:
_sendMessageByHtml();
//_sendMessageByWeb();
so to have code as these shown below:
void _incrementCounter() {
_sendMessageByHtml();
//_sendMessageByWeb();
setState(() {
Note that _sendMessageByHtml() and _sendMessageByWeb() are be used mutually exclusive; this is the reason that one of them is commented. We are going to show that the first work and the second is not working.
- Write inside project_folder/web/ a text file named "test.html" with the following content:
function onMessage(event) {
var data = event.data;
if (typeof(window[data.func]) == "function") {
window[data.func].call(null, data.message);
}
}
// Function to be called from iframe
function parentFunc(message) {
alert(">> " + message);
}
</script>
- Start the web application (device: Chrome (web)) in debug mode.
- You will see the traditional Flutter demo app. Copy the url, something like "http://localhost:47546/", open in same window of Chrome a new tab and paste copied url adding "/test.html" so to have something like "http://localhost:47546/test.html" in address bar. Refresh the page. You will see the same Flutter demo page in a small frame. Press the plus (+) button: you will see in browser an aler, a popup with ">> Sent by iframe (Html)" message.
- In main.dart change the comment so to have these lines:
void _incrementCounter() {
//_sendMessageByHtml();
_sendMessageByWeb();
setState(() {
that is commented the first call (Html) and uncommented the second call (Web).
- Refresh the Chrome tab where you have added "/test.html" to the url and press the plus button (+). The popup is no more shown.
Expected results
web.window.parent?.postMessage() must works as html.window.parent?.postMessage()
Actual results
web.window.parent?.postMessage() does nothing
Code sample
Code sample
(in steps to reproduce you will have all the code needed as code sample)Screenshots or Video
Screenshots / Video demonstration
[Upload media here]
Logs
Logs
[Paste your logs here]Flutter Doctor output
Doctor output
[Paste your output here]