I am trying to use either Geolocator.getCurrentLocation or Geolocator.checkPermission() inside Workmanager's task. Both of those calls raise the same exception:
MissingPluginException(No implementation found for method getCurrentPosition on channel flutter.baseflow.com/geolocator) - for getCurrentLocation.
And MissingPluginException(No implementation found for method checkPermission on channel flutter.baseflow.com/geolocator) for the checkPermission method.
Here is an example of the code
void callbackDispatcher() {
Workmanager().executeTask((taskName, inputData) async {
await Geolocator.checkPermission();
await Geolocator.getCurrentPosition();
});
}
There are few issues opened in GitHub repo of Geolocator, but there are no answers for them.
- https://github.com/Baseflow/flutter-geolocator/issues/1045
- https://github.com/Baseflow/flutter-geolocator/issues/1041
- https://github.com/Baseflow/flutter-geolocator/issues/1038
Any ideas on how I can solve this?
The problem is that with version 3.1.6 of the
geolocator_androidthe default method channel implementation has been replaced by a platform specific implementation. However since the task is run in a separate isolate which executes without the Flutter engine, the platform specific implementation (in this casegeolocator_android) is not registered with the platform interface (geolocator_platform_interface) resulting in theMissingPluginException.To use version 3.1.6 or later make sure you register the platform specific implementation when the
executeTaskis run.Alternatively if you are running Flutter 2.11+, you can use the new
DartPluginRegistrant.ensureInitialized()method to ensure all packages are registered correctly:More information can be found here and here.