Flutter - invoking method in method channel from a different isolate

46 views Asked by At

I am using this library to spawn a foreground service which calls a library to fetch location that invokes native code tru the use of method channels. However I am getting this error (it works perfectly when being called from the app/main isolate):

MissingPluginException(No implementation found for method getLastLocation on channel com.huawei.flutter.location/fusedlocation_methodchannel)

I saw this similar problem on this question, however the native code is coming from a library, and I don't want to modify the library as much as possible.

I also saw this article that Flutter 3.7 already supports calling natively from background isolate channels, but I'm already updated to Flutter 3.13.6. (Or is this not related to my issue?)

Questions

  • How to invoke a method from a different isolate without modifying the library
  • How to initialize Flutter Plugins programmatically, since I'm thinking if I can initialize the library on the foreground service
  • Is there any other workaround to solve this?

EDIT: Here's how I use the library, the startCallback is the entry point (and I assume this is separated from main isolate)

await FlutterForegroundTask.startService(
  notificationTitle: _environmentVariables.appName,
  notificationText: Il8n.current.foregroundTaskMessage,
  callback: startCallback,
);

@pragma('vm:entry-point')
void startCallback() {
  // wait for pre requisite before initializing foreground service scope
  // ignore: prefer-async-await
  ServiceLocator.registerPrerequisiteDependencies().then((_) {
    initForegroundServiceScope(ServiceLocator.instance);

    FlutterForegroundTask.setTaskHandler(
      MyOwnTaskHandler(), // this needed by the library, invoking functions from the foreground service
    );
  });
}

And this is how I use the Huawei's library to get location (which uses method channels upon checking their library):

final locationService = FusedLocationProviderClient();

final location = await locationService.getLastLocation(); // this is working on main isolate, but not on the foreground service
0

There are 0 answers