3

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

Kenneth
  • 3,957
  • 8
  • 39
  • 62

2 Answers2

1

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.

Kenneth
  • 3,957
  • 8
  • 39
  • 62
0

For anyone experiencing similar issues and using custom iOS-side storyboard or root view controller: make sure that FlutterViewController remains root VC and you make further native iOS navigation without replacing it.

In my similar issue, the reason of described behaviour itself was definitely in changing root view controller from FlutterViewController to my custom iOS UIViewController (reason - plugin had a reference to native SDK which used its own storyboard).

UPD: there is an issue which describes possible reasons of merely expected behaviour: https://github.com/flutter/flutter/issues/52456

kkaun
  • 603
  • 7
  • 19