gpt4 book ai didi

ios - 从 iOS 到 Native 的 Flutter MethodChannel 不起作用

转载 作者:行者123 更新时间:2023-12-01 16:19:59 28 4
gpt4 key购买 nike

我有一个 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 端什么也没有发生。没有消息,没有错误,什么都没有。我已经尝试了多种变体,包括创建多个 channel (每个 channel 一个)、动态 channel ID 等等,但它在 Flutter 方面是沉默的。有什么想法吗?

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

Edit2:它似乎在使用运行器项目从 XCode 运行时有效,但在从 Android Studio 运行 Flutter 项目时却无效。

最佳答案

如评论中所述,这突然开始工作而没有任何代码更改。我重启了我的 mac 几次,刷新了 Android Studio 等。突然间它工作了。

关于ios - 从 iOS 到 Native 的 Flutter MethodChannel 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62967887/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com