Flutter 与 JavaScript进行通信

flutter 采用的插件是flutter_inappwebview

1 APP调用js的函数

//h5端
 window.appCallJs = function(args){

            console.log(args) 
        }
//app端
 webView.injectScriptCode("window.appCallJs('hello world')");

2 js调用APP的方法

// 代码
  window.flutter_inappbrowser.callHandler('authSuccess','success',1234).then(function(result) {
                console.log(result, typeof result); 

            });
//app代码
  webView.addJavaScriptHandler('authSuccess', (args) {
                print("收到来自web的消息");
              });

//这样写,App端没有问题。但是web端存在问题,“{message: Uncaught TypeError: Cannot read property 'callHandler' of undefined, messageLevel: 3}”。

需要对callHandler进行处理,即:

<script>
    var message = "success";
    if (window.flutter_inappwebview.callHandler) {
        window.flutter_inappwebview.callHandler('authSuccess', message);
    } else {
        window.flutter_inappwebview._callHandler('authSuccess', setTimeout(function(){}), JSON.stringify([message]));
    }
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值