addJavaScriptChannel method
Adds a new JavaScript channel to the set of enabled channels.
Implementation
@override
Future<void> addJavaScriptChannel(
JavaScriptChannelParams javaScriptChannelParams,
) async {
final String channelName = javaScriptChannelParams.name;
if (_messageChannels.containsKey(channelName)) {
throw new ArgumentError(
'A JavaScriptChannel with name `$channelName` already exists.',
);
}
final channel = _messageChannels[channelName] = WebWebViewMessageChannel(
name: channelName,
params: javaScriptChannelParams,
webChannel: web.MessageChannel(),
);
final completer = Completer<bool>();
channel.webChannel.port1.onmessage = (web.MessageEvent e) {
if (e.data.toString() == 'flutter_webwebview._connected') {
completer.complete(true);
channel.webChannel.port1.onmessage = (web.MessageEvent e) {
javaScriptChannelParams.onMessageReceived(
new JavaScriptMessage(message: e.data.toString()),
);
}.toJS;
}
}.toJS;
if (_webWebViewParams.iFrame.contentWindow != null) {
_connectMessageChannel(channel);
await completer.future;
}
}