从Flutter 3.13开始,您可能已经根据您的平台通道方法调用(IOS)注意到新的Flutter 错误:

[ERROR:flutter/shell/common/shell.cc(1015)] The 'CameraController' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel.
See https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading for more information.

doc中,实现TaskQueue来处理多线程的唯一示例是为插件概念编写的:

public static func register(with registrar: FlutterPluginRegistrar) {
  let taskQueue = registrar.messenger.makeBackgroundTaskQueue()
  let channel = FlutterMethodChannel(name: "com.example.foo",
                                     binaryMessenger: registrar.messenger(),
                                     codec: FlutterStandardMethodCodec.sharedInstance,
                                     taskQueue: taskQueue)
  let instance = MyPlugin()
  registrar.addMethodCallDelegate(instance, channel: channel)
}

然而,我没有为我的iOS频道使用插件,我已经像这样初始化了AppDelegate个频道中的所有插件:

let flutterController : FlutterViewController = window?.rootViewController as! FlutterViewController                    
let cameraChannel = FlutterMethodChannel(name: "CameraController", binaryMessenger: flutterController.binaryMessenger)

然后,我在App Start上从我的原生iOS频道打电话:

private func setupCameraConst() {
 DispatchQueue.main.async {
  channel!.invokeMethod("setAspectRatio", arguments: aspectRatio)
  channel!.invokeMethod("setMinZoomLevel", arguments: minZoom)
  channel!.invokeMethod("setMaxZoomLevel", arguments: maxZoom)
  channel!.invokeMethod("setMinExposure", arguments: minExposure)
  channel!.invokeMethod("setMaxExposure", arguments: maxExposure)
  channel!.invokeMethod("setOnFrontCamera", arguments:   cameraPosition == .front)
 }
}

所以我的问题是如何处理平台线程不使用插件?

推荐答案

地址为in the official platform channel documentation:

DispatchQueue.main.async {
  // Make your invokeMethod calls here.
}

Ios相关问答推荐

将图案UI视图动画化以模拟进度条

如何加密字符串使用AES. GCM在Dart和解密相同的Swift?

AVFoundation Camera推出变焦SWIFT

许多文本字段的性能问题

如何在 Android 上的 Flutter 应用中录制内部音频?

如何在 Flutter 应用程序中测试来自 App/Play Store 的移动深层链接?

表视图(SwiftUI)中的多列?

一对多关系 Firebase 实时数据库(嵌入数百万条 comments )

如何在 SwiftUI 的 TabView 中添加底部曲线?

SwiftUI 中的偏移量和按钮有问题吗?

在所有 tableview / collectionview 中禁用滚动指示器

我可以在 Apple-Watch 上使用 iPhone 的摄像头扫描 SwiftUi 中的二维码(例如用于登录)吗

clipShape swift的三元

检测 iPhone/iPad/iPod touch 的 colored颜色 ?

UIScrollview 获取touch 事件

构建失败:ld:重复符号 _OBJC_CLASS_$_Algebra5FirstViewController

在 iOS 7 上更改标签栏色调 colored颜色

如何使用 Contacts Framework 获取 iOS 9 中的所有联系人记录

如何识别导航堆栈中的前一个视图控制器

Objective-C 将 NSDate 设置为当前 UTC