ERROR_ALREADY_REQUESTING_PERMISSIONS on flutter

Issue

I create an android app with flutter, I create permission request at the first time when app is run, so when user click deny and then click login button, permission requested again. I got this error

Exception has occurred.
PlatformException (PlatformException(ERROR_ALREADY_REQUESTING_PERMISSIONS, A request for permissions is already running, please wait for it to finish before doing another request (note that you can request multiple permissions at the same time)., null))

this is my code

@override
  void initState() {
    this.setSharedPreferences();
    PermissionHandler().checkPermissionStatus(PermissionGroup.location).then(_checkPermission);
  }

void _checkPermission(PermissionStatus status){
    if(status == PermissionStatus.unknown || status == PermissionStatus.denied){
      _askPermission();
    }
  }

void _askPermission() async{
    await PermissionHandler().requestPermissions([PermissionGroup.location]);
  }

void onLogin() async {
   PermissionStatus locationPermission = await PermissionHandler().checkPermissionStatus(PermissionGroup.location);
   if(locationPermission == PermissionStatus.denied || locationPermission == PermissionStatus.unknown){
        _askPermission();
   }else{
     // user available to login
   }
}

How to handle this? thanks for your answer

Solution

The thing is when it checks for permission it returns null instead of any of the Permission status, which results in a exception. It could possibly a bug multiple people have filled issues related to Location permission.

https://github.com/BaseflowIT/flutter-geolocator/issues/172

https://github.com/BaseflowIT/flutter-geolocator/issues/

https://github.com/BaseflowIT/flutter-permission-handler/issues

void getPermissionStatus() async {
        PermissionStatus permission = await PermissionHandler()
            .checkPermissionStatus(PermissionGroup.storage);
        if (permission == PermissionStatus.granted) {
        } // ideally you should specify another condition if permissions is denied
else if (permission == PermissionStatus.denied ||
            permission == PermissionStatus.disabled ||
            permission == PermissionStatus.restricted) {
          await PermissionHandler().requestPermissions([PermissionGroup.storage]);
          getPermissionStatus();
        }
      }

I don’t think there is problem with your code anyway you can use recursion instead of creating two separate functions.

Answered By – Nadeem Siddique

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *