Flutter-geolocator: Android - CheckGeolocationPermissionStatus executed for the first time is denied

Created on 9 Jul 2020  ·  3Comments  ·  Source: Baseflow/flutter-geolocator

In Android platform when I execute the Geolocator (). CheckGeolocationPermissionStatus () function for the first time GeolocationStatus is denied and not asking the user for permission. But in IOS when I call the same function for the first time the GeolocationStatus is unknown therefore I know I must call Geolocator (). GetCurrentPosition () to asking permission to user and return the location if permission was granted.

Is there any way to asking the user for permission in Android when I call checkGeolocationPermissionStatus for the first time?

below my code:

Future<void> getLocation() async {
    try {

      var serviceEnabled = await  Geolocator().isLocationServiceEnabled();

      if (serviceEnabled) {
        var geolocationStatus =
            await Geolocator().checkGeolocationPermissionStatus();

        if (geolocationStatus == GeolocationStatus.granted || geolocationStatus == GeolocationStatus.unknown) {
          position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
          return;
        } 
      }
    } catch (e) {
      print(e);
    }
  }

All 3 comments

Seems this plugin manager, is not more interested to maintain or support its community, i saw many issues but no accurate or bug fix solution to them.

yeah you're right! I saw other issues no answers as well. =/

@azeemgujjar, @CNogueira92, this is not the case. I am actually working hard on a next major release but as most OSS projects I need to do this in my spare time which is at the moment a bit limited so my attention is a bit split. Of course I welcome contributions to the plugin to help out.

To answer your question @CNogueira92, Android and iOS handle permissions a bit differently. By default Android treats permissions as "denied" (even when the user hasn't explicitly denied them). iOS has a specific status for the situation where permissions have not been requested yet (called "Not determined", which we translate to "unknown").

On both platforms it is fine to request permissions when the checkGeolocationPermissionStatus returns denied or unknown. If the user has selected the "Denied and don't ask again" option the Geolocator will notice this and not request for permissions.

Actually you don't even have to manually check for permissions as the Geolocator will do that internally. So you can safely update your code to:

Future<void> getLocation() async {
    try {

      var serviceEnabled = await  Geolocator().isLocationServiceEnabled();

      if (serviceEnabled) {
        position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
      } else {
        // TODO: ask the user to enable the location services...   
      }
    } catch (e) {
      print(e);
    }
  }

The checkGeolocationPermissionStatus is only a convenience method in case you want to check the permissions manually and signal the user to take some action. In the new release I mentioned you will also be able to manually request for permissions. If you would like to do that now you will need to make a call to the location_permissions plugin which is shipped with the geolocator. More information about the location_permissions plugin can be found here.

Was this page helpful?
0 / 5 - 0 ratings