美文网首页FlutterFlutter跨平台应用
HelloFlutter——MethodChannel(Nati

HelloFlutter——MethodChannel(Nati

作者: 悟笃笃 | 来源:发表于2018-12-17 14:15 被阅读128次

    在native和flutter之间,数据的交互是双向的。我们可以从Native层调用flutter层的dart代码,也可以在flutter层调用Native的代码。
    而作为通讯桥梁就是MethodChannel了,这个类在初始化的时候需要注册一个渠道值。这个值必须是唯一的,并且在使用到的Native层和Flutter层互相对应。

    MethodChannel

    使用MethodChannel进行通讯,需要在Flutter端和Native端两边做如下操作。

    • 注册渠道:在两端同时创建一个MethodChannel对象,注册相同的字符串值。
      1. Android端
            //binaryMessenger在实际开发过程中传递flutterView对象就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端注册的MethodChannel中的字符串值必须一样!!!
    • 调用一个setMethodCallHandler方法,设置MethodHandler对象,两端根据传递method字符串值去运行不同的方法
      1. Android端设置函数调用的handler,当Flutter端通过MethodChannel去调用login方法时候,在Android端会获取参数并调用
            //在android端的MethodChannel设置MethodHandler,去处理Flutter申请要调用的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端设置函数调用的handler,当Android端使用通过MethodChannel去调用callFlutter方法时候,在Flutter端会直接返回Android传入的参数
            //在Flutter端的MethodChannel设置MethodHandler,去处理Native申请要调用的method的值。
            Future<dynamic> platformCallHandler(MethodCall call) async {
                switch (call.method) {
                  case "callFlutter":
                    return arguments;
                    break;
                }
            }    
              //将上面方法生成的对象设置进去
              _platform.setMethodCallHandler(platformCallHandler);
        
    • 通讯:Native和Flutter之间的通讯是可以传递参数和获取返回值的。
      1. Android端
            //在Android端调用Flutter中的getInputParams方法,参数是以Json的格式传递。这里是无参方法使用null
            channel.invokeMethod("getInputParams", "{'arg1':'来自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端
            //调用Android端的showShortToast代码
            _platform.invokeMethod(
                'showShortToast', {'message': '$message'});
        

    相关文章

      网友评论

        本文标题:HelloFlutter——MethodChannel(Nati

        本文链接:https://www.haomeiwen.com/subject/aioqkqtx.html