0

I have a custom flutter plugin. From Flutter app I call the setMethodCallHandler on the channel which I use to invoke method on native. Flutter to native communication works along with the responses. But the reverse is not working, where the methods are invoked on native side. The native tries to call the methods but it is not able to find the callbacks as the response is always "not implemented"

The channel is defined in the Flutter plugin and I set the methodHandler during the initialization of app.

VpvpBaseModulePlugin.dart

static const platformNative = const MethodChannel('com.example.test/native_channel');

static void setMethodCallHandler(Future<dynamic> handler(MethodCall call)){
    channel.setMethodCallHandler(handler);
  }

Flutter app



Future<dynamic> handler(MethodCall methodCall) async {
    switch (methodCall.method) {
      case 'callMe':
        print("***** callMe: ${methodCall.arguments}");
        break;
    }
}

VpvpBaseModulePlugin.setMethodCallHandler(handler);

Native Call:

public lateinit var channel : MethodChannel
class VpvpBaseModulePlugin: FlutterPlugin, MethodCallHandler{

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    flutterEngine = flutterPluginBinding.flutterEngine
    channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "com.example.test/native_channel")

    channel.setMethodCallHandler(this)
    context = flutterPluginBinding.applicationContext
  }
}


Handler(Looper.getMainLooper()).post {
                            val arguments = HashMap<String, Any>()
                            arguments["errorCode"] = error.toString();
                            channel.invokeMethod("callMe", arguments)
                        }

the invocation of calls from flutter app's native module works.. but they are not working from plugin's native

user2832203
  • 85
  • 2
  • 9
  • What's the context of this line `platformNative.setMethodCallHandler(handler);` Do you have it in some sort of `initMyPlugin()` function? Is it getting called? Since your plugin will likely need to dispatch its native callbacks to the app, I normally end up calling `setMethodCallHandler` where the app sets its callback handler (in some sort of init / setHandler method) – Richard Heap Jan 19 '22 at 11:47
  • all the methods are static in my plugin. They do get called as i get response for the calls made from dart to native and their callbacks. but i do not get any response for the invocation from native of the plugin. – user2832203 Jan 19 '22 at 17:44
  • Are you sure that you are actually calling `VpvpBaseModulePlugin.setMethodCallHandler(handler);` somewhere? – Richard Heap Jan 19 '22 at 18:00
  • 1
    Check out this repo that definitely works. Here the plugin is responsible for the handler, but dispatches events through an interface implemented by the app. See: https://github.com/richardheap/audio_worklet/blob/master/lib/audio_worklet.dart and https://github.com/richardheap/audio_worklet/blob/master/example/lib/main.dart – Richard Heap Jan 19 '22 at 18:02

0 Answers0