美文网首页
MethodChannel:Flutter和Native之间的互

MethodChannel:Flutter和Native之间的互

作者: 许方镇 | 来源:发表于2019-12-06 15:24 被阅读0次

Flutter ——> Native

1.Flutter和Native定义一个相同的字符串CHANNEL_NAME

Flutter 端代码:
2、static const MethodChannel methodChannel = MethodChannel(CHANNEL_NAME);

3、final String result = await methodChannel.invokeMethod(nativeMethod, args);

Native 端代码:
4、 val channel = new MethodChannel((FlutterView)flutterView, CHANNEL_NAME)

5、

channel.setMethodCallHandler { call, result ->
                //call.method 来获取方法名
                //call.arguments 来获取入参

                //result.success来放回成功处理结果
                //result.error来放回失败后的结果
                //result.notImplemented来放回为实现该方法
            }

Native ——> Flutter

在( Flutter ——> Native)代码基础上面

Native 端代码:
1、

channel.invokeMethod(flutterMethod, args, new MethodChannel.Result () {
                @Override
                public void success(@Nullable Object o) {
                  
                }

                @Override
                public void error(String s, @Nullable String s1, @Nullable Object o) {
                    
                }

                @Override
                public void notImplemented() {
                   
                }
            });

Flutter端
2、

_channel.setMethodCallHandler((MethodCall call) {
      //call.method 来获取方法名
      //call.arguments 来获取入参
    });

相关文章

网友评论

      本文标题:MethodChannel:Flutter和Native之间的互

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