createWebMessageChannel method

Future<WebMessageChannel?> createWebMessageChannel()

Creates a message channel to communicate with JavaScript and returns the message channel with ports that represent the endpoints of this message channel. The HTML5 message channel functionality is described here.

The returned message channels are entangled and already in started state.

This method should be called when the page is loaded, for example, when the WebView.onLoadStop is fired, otherwise the WebMessageChannel won't work.

NOTE for Android: This method should only be called if AndroidWebViewFeature.isFeatureSupported returns true for AndroidWebViewFeature.CREATE_WEB_MESSAGE_CHANNEL.

NOTE for iOS: This is implemented using Javascript.

Official Android API: https://developer.android.com/reference/androidx/webkit/WebViewCompat#createWebMessageChannel(android.webkit.WebView)

Implementation

Future<WebMessageChannel?> createWebMessageChannel() async {
  Map<String, dynamic> args = <String, dynamic>{};
  Map<String, dynamic>? result =
      (await _channel.invokeMethod('createWebMessageChannel', args))
          ?.cast<String, dynamic>();
  return WebMessageChannel.fromMap(result);
}