使用flutter_webview_plugin时异步Promise的js回调flutter失效,解决方法,换库interactive_webview,flutter的大坑bug!

下面是我的js

"use strict";
exports.__esModule = true;
var signature_adapter_1 = require("@waves/signature-adapter");

window.signGetMyOrderBookHistory = function (seed,type,timestamp,senderPublicKey) {

var adapter = new signature_adapter_1.SeedAdapter(seed);


var data = {
   type: type,
     data: {
         timestamp: timestamp,
         senderPublicKey: senderPublicKey
     }
};

var signable = adapter.makeSignable(data);

signable.getSignature().then(function (signature) {
   
    const nativeCommunicator = typeof webkit !== 'undefined' ? webkit.messageHandlers.native : window.native;
    nativeCommunicator.postMessage(signature);
});

}

调用时使用flutter_webview_plugin不能发送消息给原生,但是使用interactive_webview可以发送消息,interactive_webview使用方法如下:

  static Future signGetMyOrderBookHistory(String seed,int type,int timestamp,String senderPublicKey) async {
    final _webView = new InteractiveWebView();


    String ret = '';

    try {

      _webView.stateChanged.listen((state) {
        // state.type: enum (didStatrt and didFinish)
        // state.url
        print("最新url:" + state.url);
        if(state.url.contains("http://resultData/")){
          ret = state.url;
          print("url改变" + state.url);
        }
      });

      _webView.didReceiveMessage.listen((message) {
        // message.name
        // message.data (should be a Map/List/String)
        print("js调用了flutter == ");
        print(message.data);
        ret = message.data;
      });




      var givenJS = await rootBundle.loadString('assets/html/signGetMyOrderBookHistory.js');


      await _webView.evalJavascript(givenJS);
      await _webView.evalJavascript('signGetMyOrderBookHistory(\'$seed\',$type,$timestamp,\'$senderPublicKey\');');

      while (ret.isEmpty) {
        print("ret.isEmpty");
        await Future.delayed(const Duration(seconds: 3));
        print(ret);
      }

      print("已经跳过while循环" + ret);
      if (ret != null && ret.isNotEmpty && !ret.contains("null")) {
        print("signGetMyOrderBookHistory签名后的result为:" + ret);


        BXPrint.v(ret);
        return ret;



      }else {

        BXPrint.v("signGetMyOrderBookHistory签名后的result为空");
        BXPrint.v(ret);
        return await signGetMyOrderBookHistory(seed, type, timestamp, senderPublicKey);
      }
    } catch (e) {
      BXPrint.v(e);
      BXPrint.v("signGetMyOrderBookHistory签名后的result后异常");

      return await signGetMyOrderBookHistory(seed, type, timestamp, senderPublicKey);

    }


  }
}

 

你可能感兴趣的:(flutter)