D/FlutterGeolocator( 2882): Disposing Geolocator services
E/FlutterGeolocator( 2882): Geolocator position updates stopped
D/FlutterGeolocator( 2882): Stopping location service.
I/WM-WorkerWrapper( 2882): Worker result SUCCESS for Work [ id=dc416b8a-e86b-4976-b078-9e8698ac1399, tags={ be.tramckrijte.workmanager.BackgroundWorker } ]
E/flutter ( 2882): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: MissingPluginException(No implementation found for method getCurrentPosition on channel flutter.baseflow.com/geolocator)

I tried to get current location on background. I used these packages:

workmanager: ^0.4.0
geolocator: ^8.0.0

I followed this tutorial

My code is simple
1.Call back function or static main function

//callback function
void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) async {
    await LocationService.getCurrentLocation();
    print("Native called background task-1: $task");

    return Future.value(true);
  });
}

2.init function ( I called these function on initstate)

Workmanager().initialize(
        callbackDispatcher,
        isInDebugMode: true,
      );

  Workmanager().registerPeriodicTask(
      "22",
      fetchBackground,
      initialDelay: Duration(seconds: 30),
      frequency: Duration(minutes: 30),
  );

3. final function is getcurretlocation
class LocationService {
  static getCurrentLocation() async { 
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy:LocationAccuracy.best);
    print(position );
  }
}
1

There are 1 best solutions below

0
On

Try registering the Android specific implementation when the executeTask is run.

//callback function
void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) async {
   if (defaultTargetPlatform == TargetPlatform.android) {
     GeolocatorAndroid.registerWith();
   } else if (defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.macOS) {
     GeolocatorApple.registerWith();
   } else if (defaultTargetPlatform == TargetPlatform.linux) {
     GeolocatorLinux.registerWith();
   }

   await LocationService.getCurrentLocation();
    print("Native called background task-1: $task");

    return Future.value(true);
  });
}

Alternatively if you are running Flutter 2.11+, you can use the new DartPluginRegistrant.ensureInitialized() method to ensure all packages are registered correctly:

//callback function
void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) async {
   DartPluginRegistrant.ensureInitialized();

   await LocationService.getCurrentLocation();
    print("Native called background task-1: $task");

    return Future.value(true);
  });
}

The problem is that the task is run in a separate isolate which executes without the Flutter engine. Therefore the platform specific implementations (in this case geolocator_android) is not registered with the platform interface (geolocator_platform_interface) resulting in the MissingPluginException.

More information can be found here and here.