dart调用android方法,flutter android 开发笔记(三.flutter调用android方法,并拿到返回值,展示在flutter控件或者native控件)...

1,创建flutter消息通道

mFlutter2MethodChannel = new MethodChannel(flutter2Engine.getDartExecutor(), "flutter2/flutter2Java");//字符串的含义跟JsBridge中的js和native互相调用的含义差不多,flutter中要是用这个消息通道时,也要设置为一样的

mFlutter2MethodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {

@Override

public void onMethodCall(MethodCall call, MethodChannel.Result result) {

if (call == null || result == null){

if (result!=null){

result.error("-1","MethodCall is null",new Exception("MethodCall is null"));

}

return;

}

if ("getJavaMethod".equals(call.method)){

result.success("success ");

} else {

result.success(" unKnow");

}

}

});

2,flutter调用native方法

static const platform = const MethodChannel("flutter2/flutter2Java");//跟安卓代码中设置的通道名称字符串一样

Future _getJavaMethod() async {

String str;

try {

print("dart -_getJavaMethod");

//在通道上调用此方法

final String intValue = await platform.invokeMethod("getJavaMethod");//这个参数是跟安卓协商的,相当于一个变量名,安卓用来筛选flutter需要调用的方法用的

str = ‘getJavaMethod $intValue .‘;

} on Exception catch (e) {

str = "Failed to getJavaMethod: ‘${e.toString()}‘.";

}

setState(() {

print("dart -setState");

_counter = str;

});

}

3,flutter调用结果

原文:https://www.cnblogs.com/fengchuxiaodai/p/13026440.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值