我用command做了一个flutter项目
flutter create -i swift myProject
然后我将目录更改为 myProject cd 我的项目 然后我跑 pods 设置。 它运行 swift ,但是当我根据此处的教程将代码添加到 ios/Runner/Runner 目录中的 AppDeligate.swift 时:
https://flutter.dev/docs/development/platform-integration/platform-channels
这是我的 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)
}
}
我收到错误消息“‘FlutterViewController’类型的值没有成员‘binaryMessenger’”
我错过了什么吗?
最佳答案
改变这一行
let batteryChannel = FlutterMethodChannel(name: "samples.flutter.dev/battery",
binaryMessenger: controller.binaryMessenger)
至 ->
let batteryChannel = FlutterMethodChannel(name: "samples.flutter.dev/battery",
binaryMessenger: controller as! FlutterBinaryMessenger)
在最新的 flutter 升级中已更改。
关于ios - 在 flutter 项目中编写 ios native swift 代码时,Xcode 给出错误值类型 'FlutterViewController' 没有成员 'binaryMessenger',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58371680/