jnschulze / flutter-webview-windows

A WebView2-powered Flutter WebView implementation for the Windows platform.
BSD 3-Clause "New" or "Revised" License
203 stars 120 forks source link

postWebMessage failed #277

Open summanzhao opened 8 months ago

summanzhao commented 8 months ago

Hello, I would like to send a message to JavaScript using the postWebMessage method:

WebviewController kWebController=WebviewController();

//Several initialization tasks for WebWiew ........ _ Subscriptions. add (kWebController. loadingState. listen ((state)){

DebugPrint ('Loading state: $state ');

If (state==LoadingState. navigation Completed){

KWebController.executeScript(

"Window. addEventListener ('message ', event=>{console. log ('Received message:', event. data);});";

KWebController. postWebMessage ("hello world");

}

});

After completion

KWebController. postWebMessage ("hello world");

There was an error in this call: [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(not_supported, Posting the message failed., null, null)

0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652:7)

1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:310:18)

How should I solve it? Looking forward to your reply!
bladeofgod commented 6 months ago

same issue.
And can you post msg from web ?

xorespesp commented 5 months ago

Hello, I would like to send a message to JavaScript using the postWebMessage method:

WebviewController kWebController=WebviewController();

//Several initialization tasks for WebWiew ........ _ Subscriptions. add (kWebController. loadingState. listen ((state)){

DebugPrint ('Loading state: $state ');

If (state==LoadingState. navigation Completed){

KWebController.executeScript(

"Window. addEventListener ('message ', event=>{console. log ('Received message:', event. data);});";

KWebController. postWebMessage ("hello world");

}

});

After completion

KWebController. postWebMessage ("hello world");

There was an error in this call: [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(not_supported, Posting the message failed., null, null) #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652:7) #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:310:18)

How should I solve it? Looking forward to your reply!

The WebviewController.postWebMessage method internally calls ICoreWebView2.PostWebMessageAsJson, so the message passed to the WebviewController.postWebMessage method must be in JSON format. otherwise, it will fail internally with HRESULT 0x80070057(E_INVALIDARG) error.

... also if you want to receive messages on the JavaScript side, you need to use window.chrome.webview.addEventListener, not Window.addEventListener.

Try this:

WebviewController _webviewController = WebviewController();

// ... 

_subscriptions.add(_webviewController.loadingState.listen((state) {
  debugPrint('Loading state: $state ');

  if (state == LoadingState.navigationCompleted) {
    _webviewController.executeScript(
      "window.chrome.webview.addEventListener('message', event => { console.log('Received message:', event.data.message); });"
    );
    _webviewController.postWebMessage("{ \"message\": \"hello world\" }");
  }
}));

See: