【问题标题】:Flutter MethodChannel from iOS to Native not working从 iOS 到 Native 的 Flutter MethodChannel 不起作用
【发布时间】:2020-11-08 01:57:12
【问题描述】:

我有一个 Flutter 项目试图在 Flutter 和 iOS Native 之间建立双向通信。到目前为止,它从 FlutteriOS 都运行良好,但反之则无声。当我从 iOS 调用该方法时,没有任何反应。没有消息,没有错误,什么都没有。

我在 iOS 上创建 MethodChannel,如下所示:

let channel = FlutterMethodChannel(
    name: "paymentview",
    binaryMessenger: controller.binaryMessenger
)

   channel.setMethodCallHandler({
       (call: FlutterMethodCall, result: FlutterResult) -> Void in
       if (call.method == "initialize") {

       } else if(call.method == "authorize") {

        }
   })

Flutter 方面就像这样:

_channel = new MethodChannel('paymentview');
_channel.setMethodCallHandler(methodCallHandler);

Future<dynamic> methodCallHandler(MethodCall methodCall) async {
    switch (methodCall.method) {
      case 'authorized':
        print("authorized");
        return null;
      default:
        throw PlatformException(code: 'Not implemented', message: 'Not implemented');
    }
  }

Flutter 上我调用这样的方法:

_channel.invokeMethod('authorize');

而且效果很好。在 iOS 中收到呼叫。但在 iOS 端,我调用这样的方法:

DispatchQueue.main.async {
            self.channel.invokeMethod("authorized", arguments: nil)
        }

Flutter 方面什么也没有发生。没有消息,没有错误,没有什么。我尝试了许多变体,创建多个通道(每种方式一个)、动态通道 ID 等等,但它在 Flutter 方面保持沉默。有什么想法吗?

编辑:我已经从 iOS 端调试了它,它确实有效。我从我的 Dart 代码中得到返回值。但是,当我调试 Dart 代码时,没有遇到断点。如果我尝试修改 Dart 中的某些状态,则什么也不会发生...

Edit2:当从 XCode 运行时,使用 runner 项目,它似乎可以工作,但在从 Android Studio 运行 Flutter 项目时却不行。

【问题讨论】:

  • 我遇到了类似的问题,你解决了吗?
  • 嘿。我对这个“解决方案”感到非常抱歉,但它在几次重启和 Android Studio 缓存失效等后突然开始工作。我没有很好的解释:/

标签: ios flutter native flutter-method-channel


【解决方案1】:

正如 cmets 中提到的,这突然开始工作而没有任何代码更改。我重启了我的 mac 几次,刷新了 Android Studio 等。突然它工作了。

【讨论】:

  • 和我做同样的事情
猜你喜欢
  • 2023-02-10
  • 1970-01-01
  • 2022-01-14
  • 2020-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多