1

Is it possible to call a native function using MethodChannel from an Isolate? The documentation only shows how to adapt the native code in the case of plugins. However, I'm using MethodChannel as described in the page linked above, without creating a plugin.

Code in Flutter Isolate:

DartPluginRegistrant.ensureInitialized();
const methodChannel = MethodChannel('MethodChannel');
final double result = await methodChannel.invokeMethod('getAltitude');

Code in MainActivity.kt:

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {    
    GeneratedPluginRegistrant.registerWith(flutterEngine); // saw this in another thread.  Tested with and without it    
    super.configureFlutterEngine(flutterEngine)    
    val taskQueue = flutterEngine.dartExecutor.binaryMessenger.makeBackgroundTaskQueue()

     MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "MethodChannel", StandardMethodCodec.INSTANCE,taskQueue).setMethodCallHandler {
      call, result ->
      if (call.method == "getAltitude") {
        val altitude = "xxx"
        result.success(altitude)
       
      } else {
        result.notImplemented()
      }
    }    
    
  }

This is the error I get:

MissingPluginException (MissingPluginException(No implementation found for method getAltitude on channel MethodChannel))
cpper
  • 142
  • 2
  • 13
  • 1
    Since it is so easy to create a plugin project - and then to depend your main project on that plugin project, why wouldn't you do that if it makes the plugin code easier? – Richard Heap Mar 04 '23 at 00:59
  • 1
    I just did this, I created a plugin for the getAltitude function. Surprisingly, I can use the plugin from an isolate without adapting the code as specified in the docs section I linked (so without using task queue). – cpper Mar 05 '23 at 19:50
  • i want to know this as well – smedasn May 04 '23 at 14:34

0 Answers0