Flutter筆記——MethodChannel(Native&Flutter數(shù)據(jù)交互)

在native和flutter之間,數(shù)據(jù)的交互是雙向的。我們可以從Native層調(diào)用flutter層的dart代碼,也可以在flutter層調(diào)用Native的代碼。
而作為通訊橋梁就是MethodChannel了,這個(gè)類(lèi)在初始化的時(shí)候需要注冊(cè)一個(gè)渠道值。這個(gè)值必須是唯一的,并且在使用到的Native層和Flutter層互相對(duì)應(yīng)。

MethodChannel

使用MethodChannel進(jìn)行通訊,需要在Flutter端和Native端兩邊做如下操作。

  • 注冊(cè)渠道:在兩端同時(shí)創(chuàng)建一個(gè)MethodChannel對(duì)象,注冊(cè)相同的字符串值。
    1. Android端
          //binaryMessenger在實(shí)際開(kāi)發(fā)過(guò)程中傳遞flutterView對(duì)象就ok了
          MethodChannel channel = new MethodChannel(binaryMessenger, "cn.wzh.withflutter.plugins.flutter");        
      
    2. Flutter端
          static const _platform =
              const MethodChannel("cn.wzh.withflutter.plugins.flutter");
      
    在Native和Flutter端注冊(cè)的MethodChannel中的字符串值必須一樣!!!
  • 調(diào)用一個(gè)setMethodCallHandler方法,設(shè)置MethodHandler對(duì)象,兩端根據(jù)傳遞method字符串值去運(yùn)行不同的方法
    1. Android端設(shè)置函數(shù)調(diào)用的handler,當(dāng)Flutter端通過(guò)MethodChannel去調(diào)用login方法時(shí)候,在Android端會(huì)獲取參數(shù)并調(diào)用
          //在android端的MethodChannel設(shè)置MethodHandler,去處理Flutter申請(qǐng)要調(diào)用的method的值。
          channel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
              channel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
                  @Override
                  public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
                      switch (methodCall.method) {
                          case NATIVE_METHOD_LOGIN:
                              login(context, (String) methodCall.argument("phone"), (String) methodCall.argument("code"));
                              break;
                      }
                  }
              });
          });
          private static void login(Context context, String phone, String code) {
              Toast.makeText(context, "phone = " + phone + " -》  code = " + code, Toast.LENGTH_SHORT).show();
          }
      
    2. Flutter端設(shè)置函數(shù)調(diào)用的handler,當(dāng)Android端使用通過(guò)MethodChannel去調(diào)用callFlutter方法時(shí)候,在Flutter端會(huì)直接返回Android傳入的參數(shù)
          //在Flutter端的MethodChannel設(shè)置MethodHandler,去處理Native申請(qǐng)要調(diào)用的method的值。
          Future<dynamic> platformCallHandler(MethodCall call) async {
              switch (call.method) {
                case "callFlutter":
                  return arguments;
                  break;
              }
          }    
            //將上面方法生成的對(duì)象設(shè)置進(jìn)去
            _platform.setMethodCallHandler(platformCallHandler);
      
  • 通訊:Native和Flutter之間的通訊是可以傳遞參數(shù)和獲取返回值的。
    1. Android端
          //在Android端調(diào)用Flutter中的getInputParams方法,參數(shù)是以Json的格式傳遞。這里是無(wú)參方法使用null
          channel.invokeMethod("getInputParams", "{'arg1':'來(lái)自Native'}", object : MethodChannel.Result {
              override fun notImplemented() {
                  TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
              }
      
              override fun error(p0: String?, p1: String?, p2: Any?) {
                  TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
              }
      
              override fun success(p0: Any?) {
                  Toast.makeText(this@FlutterPageActivity, p0.toString(), Toast.LENGTH_SHORT).show()
              }
          })
      
    2. Flutter端
          //調(diào)用Android端的showShortToast代碼
          _platform.invokeMethod(
              'showShortToast', {'message': '$message'});
      
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容