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.
Officially Supported Platforms/Implementations:
- Android WebView (Official API - WebViewCompat.createWebMessageChannel):
- This method should only be called if WebViewFeature.isFeatureSupported returns
truefor WebViewFeature.CREATE_WEB_MESSAGE_CHANNEL.
- This method should only be called if WebViewFeature.isFeatureSupported returns
- iOS WKWebView:
- This method is implemented using JavaScript.
- macOS WKWebView:
- This method is implemented using JavaScript.
- Linux WPE WebKit:
- Implemented via JavaScript MessageChannel API.
- Windows WebView2:
- Implemented via JavaScript MessageChannel API.
Use the PlatformInAppWebViewController.isMethodSupported method to check if this method is supported at runtime.
Implementation
Future<WebMessageChannel?> createWebMessageChannel() async {
final webMessagePlatform = await platform.createWebMessageChannel();
if (webMessagePlatform == null) {
return null;
}
return WebMessageChannel.fromPlatform(platform: webMessagePlatform);
}