postWebMessage method Null safety

Future<void> postWebMessage(
  1. {required WebMessage message,
  2. Uri? targetOrigin}
)

Post a message to main frame. The embedded application can restrict the messages to a certain target origin. See HTML5 spec for how target origin can be used.

A target origin can be set as a wildcard ("*"). However this is not recommended.

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

NOTE for iOS: This is implemented using Javascript.

Official Android API: https://developer.android.com/reference/androidx/webkit/WebViewCompat#postWebMessage(android.webkit.WebView,%20androidx.webkit.WebMessageCompat,%20android.net.Uri)

Implementation

Future<void> postWebMessage(
    {required WebMessage message, Uri? targetOrigin}) async {
  if (targetOrigin == null) {
    targetOrigin = Uri.parse("");
  }
  Map<String, dynamic> args = <String, dynamic>{};
  args.putIfAbsent('message', () => message.toMap());
  args.putIfAbsent('targetOrigin', () => targetOrigin.toString());
  await _channel.invokeMethod('postWebMessage', args);
}