Search code examples
flutterapigeolocationflutter-dependenciesgeolocator

Geolocator package not working in Flutter


has any one of you worked with the geolocator package? if yes then can you please help. I have done all the steps mentioned in the docs of the geolocator package excluding the androidx conversion I think the error that I am getting is that the app is not able to get the location. The permission is not being granted I don't know why enter image description here

enter image description here

i'm currently testing it on an emulator not a real device.

i have tried making the changes in the android manifest and the info.plist for both ios and android but I don't know why it's not working. One time when I ran the app it said something like the API was outdated or so, but that error did not occur after I rebuild the whole app. if you know what is wrong and require some other info regarding my code. Please feel free to comment and I'll provide you with the necessary details.


Solution

  • **

    Did you try this ?

    **

     serviceEnabled = await Geolocator.isLocationServiceEnabled();
    
        if (serviceEnabled) {
          serviceEnabled = true;
        } else {
          serviceEnabled = false;
        }
    
        LocationPermission _permission = await Geolocator.checkPermission();
    
        if (_permission == LocationPermission.denied) {
          _permission = await Geolocator.requestPermission();
          if (_permission == LocationPermission.always || _permission == LocationPermission.whileInUse) {
            permissionGranted = true;
          } else {
            permissionGranted = false;
          }
        } else {
          if (_permission == LocationPermission.always || _permission == LocationPermission.whileInUse) {
            permissionGranted = true;
          } else {
            permissionGranted = false;
          }
        }
    
        if (serviceEnabled && permissionGranted) {
          try {
            locationData = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.bestForNavigation, timeLimit: const Duration(seconds: 4));
          } catch (e) {}
        }