3

iam trying to open a flutter screen from native android

so iam trying to use MethodChannel for returning data to dart then invoke method that's gonna navigate me to the current screen

but my code is not working

this is my code

@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    super.configureFlutterEngine(flutterEngine);
    String channelName = "app_channel";
    MethodChannel methodChannel = new MethodChannel(flutterEngine.getDartExecutor(), channelName);

    methodChannel.invokeMethod("openCaller", false, new MethodChannel.Result() {
        @Override
        public void success(@Nullable Object result) {
            Log.i("fromInvoke","success" + result.toString());
        }

        @Override
        public void error(String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails) {
            Log.i("fromInvoke","failed" + errorMessage);

        }

        @Override
        public void notImplemented() {
            Log.i("fromInvoke","not implemented");

        }
    });
}

}

  static const platform =
      const MethodChannel('app_channel');

  @override
  void initState() {
    super.initState();
    platform.setMethodCallHandler(invokedMethods);

  }

and this is a global function

 Future<dynamic> invokedMethods(MethodCall methodCall) async {
    switch (methodCall.method) {
        case "openCaller":
          print("arrived to open caller");
          // Navigator.pushNamed(context, "/ring");
          
      }
  }
Muhammad
  • 601
  • 2
  • 9
  • 18

1 Answers1

0

The method methodChannel.invokeMethod("openCaller", ...) doesn't seem to be triggered on the code snippets you've provided, it's only set inside configureFlutterEngine. You need to invoke the method from Android to call the function you've configured inside the Flutter app. Check this article covering this topic in more detail and with a sample code.

Omatt
  • 8,564
  • 2
  • 42
  • 144