参考资料:https://blog.csdn.net/julystroy/article/details/87367520
举个例子,我们想从Native端请求Flutter端的一个getFlutterName方法获取一个字符串。在Flutter端你需要给MethodChannel设置一个MethodCallHandler:
platform.setMethodCallHandler(platformCallHandler); Future<dynamic> platformCallHandler(MethodCall call) async { switch (call.method) { case "getFlutterName": return "Flutter name flutter"; break; } }
Dart端:核心关键就是直接return 需要返回的数据即可!
Java端:就是用CallBack的形式来接收返回的数据:
mChannel.invokeMethod(RCMethodList.MethodCallBackGetUserInfo, userId, new Result() { @Override public void success(@Nullable Object arg) { if (arg instanceof Map) { Map map = (Map) arg; String userId = (String) map.get("userId"); String name = (String) map.get("name"); String portraitUri = (String) map.get("portraitUrl"); UserInfo userInfo = new UserInfo(userId, name, Uri.parse(portraitUri)); flutterCallBack.success(userInfo); } } @Override public void error(String s, @Nullable String s1, @Nullable Object o) { flutterCallBack.error("获取用户信息失败:" + s, null); } @Override public void notImplemented() { flutterCallBack.error("flutter 端没有实现getUserInfoByUserId接口", null); } });
原文地址:https://www.cnblogs.com/hbolin/p/11169020.html
时间: 2024-10-06 15:55:09