fluttercommunity / flutter_webview_plugin

Community WebView Plugin - Allows Flutter to communicate with a native WebView.
https://pub.dev/packages/flutter_webview_plugin
Other
1.48k stars 929 forks source link

Documentation request for javascriptChannels #851

Closed themisir closed 3 years ago

themisir commented 3 years ago

Use case

Currently javascriptChannels is not documented well.

Proposal

For example it's not documented how to pass messages to js channel from html/js side to flutter.

PaoloP98 commented 3 years ago

From JS (I pass json string formatted as json):

let channelName = "FlutterChannel"
window["FlutterChannel"].postMessage('{"method": "myDartMethod"}');

From dart:

WebviewScaffold(
  javascriptChannels: Set.from([
  JavascriptChannel(
    name: 'FlutterChannel',
    onMessageReceived: (JavascriptMessage message) {
    final decoded = json.decode(message.message);
    String method = decoded["method"];
    if (method == "myDartMethod") {
      [your code here]
    }
  ])
themisir commented 3 years ago

Thanks. So in JS I can post message using

window.[JavascriptChannel.name].postMessage(content);

In previous versions I had to send message with channel name to special function.