如何使用MethodChannel从本机平台调用应用程序的Dart部分中的方法? [英] How to call methods in Dart portion of the app, from the native platform using MethodChannel?

查看:166
本文介绍了如何使用MethodChannel从本机平台调用应用程序的Dart部分中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个本机插件,在某些情况下,该插件必须调用应用程序的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<dynamic> handler(MethodCall call)),因此我们需要在Dart端提供一个返回Future<dynamic>的函数,例如_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);

然后实现处理程序.这处理两种方法foobar分别返回Stringdouble.

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 NSObjectFlutterMethodNotImplemented表示未实现).

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆