initChannelFunctions method

  1. @override
void initChannelFunctions()
override

Implementation

@override
void initChannelFunctions() {
  JavascriptRuntime.channelFunctionsRegistered[_instanceId] = {};

  // Setup sendMessage function in global scope
  _evaluateJS('''
    (function() {
      window.__flutter_js_sendMessage = function(channelName, message) {
        if (window.__flutter_js_channels && window.__flutter_js_channels[channelName]) {
          try {
            const result = window.__flutter_js_channels[channelName](JSON.parse(message));
            return result !== undefined ? JSON.stringify(result) : null;
          } catch (e) {
            console.error('Error in channel ' + channelName + ':', e);
            return null;
          }
        } else {
          console.warn('No channel registered: ' + channelName);
          return null;
        }
      };
    })();
  ''');
}