addJavaScriptChannel method

Future<void> addJavaScriptChannel(
  1. String name, {
  2. required void onMessageReceived(
    1. JavaScriptMessage
    ),
})

Adds a new JavaScript channel to the set of enabled channels.

The JavaScript code can then call postMessage on that object to send a message that will be passed to onMessageReceived.

For example, after adding the following JavaScript channel:

final WebViewController controller = WebViewController();
controller.addJavaScriptChannel(
  name: 'Print',
  onMessageReceived: (JavascriptMessage message) {
    print(message.message);
  },
);

JavaScript code can call:

Print.postMessage('Hello');

to asynchronously invoke the message handler which will print the message to standard output.

Adding a new JavaScript channel only takes affect after the next page is loaded.

A channel name cannot be the same for multiple channels.

Implementation

Future<void> addJavaScriptChannel(
  String name, {
  required void Function(JavaScriptMessage) onMessageReceived,
}) {
  assert(name.isNotEmpty);
  return platform.addJavaScriptChannel(JavaScriptChannelParams(
    name: name,
    onMessageReceived: onMessageReceived,
  ));
}