Communication between 2 flutter apps (mobile and web) with webview

39 Views Asked by At

I have 2 flutter apps: 1 mobile app and 1 web app. I display the web app in my mobile app with a webview. I can send message from web app to mobile app. But i don't understand how to answer with a message from mobile app to web app

To send a message from web app to mobile app, i use a channel 'awc':

js.context.callMethod('eval', ['window.awc.postMessage("get_endpoint");']);

In the mobile app, i have the webview and the onMessageReceived method. I receive correctly the message

_webViewController = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..addJavaScriptChannel(
        'awc',
        onMessageReceived: (JavaScriptMessage message) async {
          print('message ${message.message}');
          await _webViewController!
              .runJavaScript("window.postMessage('answer', '*');");
          );
        },
      )
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            // Update loading bar.
          },
          onPageStarted: (String url) {},
          onPageFinished: (String url) {},
          onWebResourceError: (WebResourceError error) {
            log('$error');
          },
          onNavigationRequest: (NavigationRequest request) {
            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse('http://localhost:59295/'));

But i don't konw how to get in the flutter web app the 'answer' just after my js.context.callMethod('eval', ['window.awc.postMessage("get_endpoint");']);

0

There are 0 best solutions below