[英]Xcode giving error Value of type 'FlutterViewController' has no member 'binaryMessenger' when writing ios native swift code in flutter project
I made a flutter project with command我用命令做了一个 flutter 项目
flutter create -i swift myProject
then I changed the directory to myProject with cd myProject and ther I run pod setup.然后我使用 cd myProject 将目录更改为 myProject,然后运行 pod setup。 It run swiftly but when I added code to AppDeligate.swift in ios/Runner/Runner directory according to the tutorial here:它运行迅速,但是当我根据此处的教程将代码添加到 ios/Runner/Runner 目录中的 AppDeligate.swift 时:
https://flutter.dev/docs/development/platform-integration/platform-channels https://flutter.dev/docs/development/platform-integration/platform-channels
Here is my AppDeligate.swift:这是我的 AppDeligate.swift:
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let batteryChannel = FlutterMethodChannel(name: "samples.flutter.dev/battery",
binaryMessenger: controller.binaryMessenger)
batteryChannel.setMethodCallHandler({
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
// Note: this method is invoked on the UI thread.
// Handle battery messages.
})
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
I am getting error "Value of type 'FlutterViewController' has no member 'binaryMessenger'"我收到错误消息“‘FlutterViewController’类型的值没有成员‘binaryMessenger’”
Am I missing something?我错过了什么吗?
Change this line更改此行
let batteryChannel = FlutterMethodChannel(name: "samples.flutter.dev/battery",
binaryMessenger: controller.binaryMessenger)
To ->至 ->
let batteryChannel = FlutterMethodChannel(name: "samples.flutter.dev/battery",
binaryMessenger: controller as! FlutterBinaryMessenger)
It was changed in latest flutter upgrade.在最新的 flutter 升级中进行了更改。
When using Flutter MACOS platform this will not help, you need to use this code:当使用 Flutter MACOS 平台时,这将无济于事,您需要使用以下代码:
let batteryChannel = FlutterMethodChannel(
name: "samples.flutter.dev/battery",
binaryMessenger: controller.engine.binaryMessenger
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.