createWebMessageChannel method
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 PlatformWebViewCreationParams.onLoadStop is fired, otherwise the PlatformWebMessageChannel won't work.
NOTE for Android native WebView: This method should only be called if WebViewFeature.isFeatureSupported returns true
for WebViewFeature.CREATE_WEB_MESSAGE_CHANNEL.
NOTE for iOS: it is implemented using JavaScript.
NOTE for MacOS: it is implemented using JavaScript.
Officially Supported Platforms/Implementations:
- Android native WebView (Official API - WebViewCompat.createWebMessageChannel)
- iOS
- MacOS
Implementation
Future<WebMessageChannel?> createWebMessageChannel() async {
final webMessagePlatform = await platform.createWebMessageChannel();
if (webMessagePlatform == null) {
return null;
}
return WebMessageChannel.fromPlatform(platform: webMessagePlatform);
}