Search code examples
iosswiftflutternative

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 create -i swift myProject

then I changed the directory to myProject with cd myProject and ther I run pod setup. It run swiftly but when I added code to AppDeligate.swift in ios/Runner/Runner directory according to the tutorial here:

https://flutter.dev/docs/development/platform-integration/platform-channels

Here is my 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'"

Am I missing something?


Solution

  • 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.