今日,有时间做Flutter测试用例,发现在MethodChannel中存在invokeMethod的方法,如:
MethodChannel.invokeMethod(methodName, argument)
原生端完整示例:
const val CONNECTION_CHANNEL = "com.demo.flutter.connection"
val methodChannel = MethodChannel(flutterView, CONNECTION_CHANNEL)
methodChannel.setMethodCallHandler { methodCall, result ->
when (methodCall.method) {
"activityFinish" -> {
goBack() //关闭当前页面
result.success(null) //标识调用成功
}
"showToastMessage" -> showShortToast(methodCall.argument<String>("message"))
}
}
Handler().postDelayed({
methodChannel.invokeMethod("aaa", "c")
}, 5000)
Flutter端示例:
static const _platform = const MethodChannel("com.whitehorse.flutter.connection");
_AboutPageState() {
_platform.setMethodCallHandler((handler) {
switch (handler.method) {
case "aaa":
_platform.invokeMethod("showToastMessage", {"message": "您调用了dart里的方法"});
break;
}
});
}
void _goBack() {
// _platform.invokeMethod("activityFinish");
}
先就做这样一个简单的记录,如果有兴趣还不是很明白的朋友可以在简书上留言!