如何使用 MethodChannel 从本机平台调用应用程序的 Dart 部分中的方法? [英] How to call methods in Dart portion of the app, from the native platform using MethodChannel?
问题描述
我正在编写一个原生插件,在某些情况下,它必须调用应用程序的 Flutter 部分中的函数,用 Dart 编写.它是如何实现的,解释如下:https://flutter.io/platform-channels/
I am writing a native plugin that, in some cases, has to call functions in the Flutter portion of the app, written in Dart. How it's achieved, is explained here: https://flutter.io/platform-channels/
此外,这里有一个从原生/平台部分向 Dart/非原生调用方法的示例:https://github.com/flutter/plugins/tree/master/packages/quick_actions
Furthermore, an example of invoking a method from the native/platform part towards the Dart/non-native is here: https://github.com/flutter/plugins/tree/master/packages/quick_actions
现在,这个例子非常好,以防平台只需要调用一个method
,即该调用不返回任何内容/void
,但万一它需要调用一个 function
,即需要来自非本机/Dart 部分的返回值,我在互联网上找不到示例或文档.不过我相信它是可以实现的,因为在原生 Java 部分,有一个方法:
Now, this example is really nice in case the platform only needs to invoke a method
, i.e. that call returns nothing/void
, but in case it needs to invoke a function
, i.e. needs a return value from the non-native/Dart part, I could not have found an example or documentation on the internet. I believe it can be implemented though, because in the native Java part, there is a method:
public void invokeMethod(String method, Object arguments, MethodChannel.Result callback)
所以,有一个 callback
对象可能有来自非本地部分的返回值 - 或者,我在这里弄错了,目前没有办法从非本地部分返回值- 应用的原生 Dart 部分?
So, there is a callback
object that could have a return value from the non-native part - or, I am mistaken here, and there is currently no way of returning a value from the non-native Dart portion of the app?
推荐答案
签名是void setMethodCallHandler(Future
,所以我们需要在返回 Future
的 Dart 端,例如 _channel.setMethodCallHandler(myUtilsHandler);
The signature is void setMethodCallHandler(Future<dynamic> handler(MethodCall call))
, so we need to provide a function at the Dart end that returns Future<dynamic>
, for example _channel.setMethodCallHandler(myUtilsHandler);
然后实现处理程序.这个处理两个方法 foo
和 bar
分别返回 String
和 double
.
Then implement the handler. This one handles two methods foo
and bar
returning respectively String
and double
.
Future<dynamic> myUtilsHandler(MethodCall methodCall) async {
switch (methodCall.method) {
case 'foo':
return 'some string';
case 'bar':
return 123.0;
default:
throw MissingPluginException('notImplemented');
}
}
在Java端,返回值被传递给Result
回调的success
方法.
At the Java end the return value is passed to the success
method of the Result
callback.
channel.invokeMethod("foo", arguments, new Result() {
@Override
public void success(Object o) {
// this will be called with o = "some string"
}
@Override
public void error(String s, String s1, Object o) {}
@Override
public void notImplemented() {}
});
在 Swift 中,返回值是传递给 result
闭包的 Any?
.(未实现由任何参数表示为 const NSObject
值 FlutterMethodNotImplemented
.)
In Swift, the return value is an Any?
passed to the result
closure. (Not implemented is signaled by the any parameter being the const NSObject
value FlutterMethodNotImplemented
.)
channel.invokeMethod("foo", arguments: args, result: {(r:Any?) -> () in
// this will be called with r = "some string" (or FlutterMethodNotImplemented)
})
这篇关于如何使用 MethodChannel 从本机平台调用应用程序的 Dart 部分中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!