从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)
}
}
所以我的问题是如何处理平台线程不使用插件?