iosflutternativeflutter-method-channel

Flutter MethodChannel from iOS to Native not working


I have a Flutter project trying to establish two-way communication between Flutter and iOS Native. So far it's working perfectly from Flutter to iOS, but the other way around it's silent. When I call the method from iOS, nothing happens. No message, no errors, no nothing.

I create the MethodChannel on iOS like this:

let channel = FlutterMethodChannel(
    name: "paymentview",
    binaryMessenger: controller.binaryMessenger
)

   channel.setMethodCallHandler({
       (call: FlutterMethodCall, result: FlutterResult) -> Void in
       if (call.method == "initialize") {

       } else if(call.method == "authorize") {

        }
   })

And like this on the Flutter side:

_channel = new MethodChannel('paymentview');
_channel.setMethodCallHandler(methodCallHandler);

Future<dynamic> methodCallHandler(MethodCall methodCall) async {
    switch (methodCall.method) {
      case 'authorized':
        print("authorized");
        return null;
      default:
        throw PlatformException(code: 'Not implemented', message: 'Not implemented');
    }
  }

On Flutter I invoke a method like this:

_channel.invokeMethod('authorize');

And that works fine. The call is received in iOS. But on the iOS side I call a method like this:

DispatchQueue.main.async {
            self.channel.invokeMethod("authorized", arguments: nil)
        }

And nothing happens on the Flutter side. No message, no error, no nothnig. I've tried numerous variants, with creating multiple channels (one each way), dynamic channel ids, and so on, but it's silent on the Flutter side. Any ideas?

Edit: I've debugged this from the iOS side, and it does work. I get the return value from my Dart code. However, when I debug the Dart code, no breakpoints are hit. If I try to modify some state in Dart, nothing happens...

Edit2: It seems to work when run from XCode, using the runner project, but not when running the Flutter project from Android Studio..


Solution

  • As mentioned in the comments, this suddenly started working without any code changes. I rebooted my mac a few times, refreshed Android Studio, etc. And suddenly it worked.