How to send postmessage in Web to flutter?

86 views Asked by At

How to send postmessage inWeb to flutter?

  • first there is my code. It's worked in Flutter_Web_View Version 3 But Doesn't work anymore cuz I upgrade it Flutter_Web_View Version 4

  • and console logged TypeError: Cannot read properties of undefined (reading 'postMessage')

1. send data and trigger JSchannel method in Flutter From Web

    try {
        window._isLoggin.postMessage("_isLoggin");  // <= error from here
        window.addEventListener('')
      } catch (e) {
        alert({
          alertType: "Fail",
          text: e,
        });
        isLoading.value = false;
        return;
      }

2.

...

@override
  void initState() {
    super.initState();
    webViewController = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Color(0x00000000))
      ..loadRequest(Uri.parse('http://myWebURL:portNum/'))
      ..addJavaScriptChannel('_isLoggin',
          onMessageReceived: (JavaScriptMessage message) async {
        List<String> messages = message.message.split(' ');
        print('received messages : $messages');
        _isLoggin();
      });
  }

  void _isLoggin() async {
    print('loggin successfully');
    ...
    if (result.isNotEmpty) {
      webViewController.runJavaScript('window.hasUserData("true $_token")');
    } else {
      webViewController.runJavaScript('window.hasUserData("false $_token")');
    }
  }

  @override
  Widget build(BuildContext context) {
    return WebViewWidget(
                controller: webViewController,
           );
    );
  }
  • So How can I connect between flutter and Web?

  • I'd appreciate it if you could point out the problem with my code or give me a simple example.

Thank you for reading the long question.

0

There are 0 answers