e answer of this problem is for me to change the geolocator version in pubspec.yml to 7.0.0

202 views Asked by At

E/flutter (13537): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: MissingPluginException(No implementation found for method isLocationServiceEnabled on channel flutter.baseflow.com/geolocator) E/flutter (13537): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:154:7) E/flutter (13537): E/flutter (13537): #1 _gettinglocationState._determinePosition (package:flutter_app/main.dart:34:22) E/flutter (13537): E/flutter (13537):

1

There are 1 answers

0
Maurits van Beusekom On

I am assuming you get this error when trying to acquire the current position from a background task or isolate.

The problem with this is that with version 3.1.6 of the geolocator_android and version 2.1.2 of the geolocator_apple (iOS and macOS) the 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 case geolocator_android) is not registered with the platform interface (geolocator_platform_interface) resulting in the MissingPluginException.

To use geolocator_android version 3.1.6+ or geolocator_apple version 2.1.2+
(these are dependencies of geolocator version 8.0.0) make sure you register the platform specific implementation when the background task is started. Examples on how to accomplish this using the Workmanager are:

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 Geolocator.checkPermission();
    await Geolocator.getCurrentPosition();
  });
}

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

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

    await Geolocator.checkPermission();
    await Geolocator.getCurrentPosition();
  });
}

More information can be found here and here.