Permission request flutter app after denial

Question

Asked by Mattia on November 23, 2021 (source).

I'm developing a flutter application, I need to manage the permission request, but I don't know how to treat a particular occurrence:

If I deny two times the same permission through the popup it could be impossible approve it later 'cause the popup will not appair again.

Future<void> requestStoragePermission() async{
  var status = await Permission.storage.status;
  if(status.isPermanentlyDenied){
    await AppSettings.openAppSettings();
  } else {
    await Permission.storage.request();
  }
}

I don't understand how to distinguish when the permission has not yet been granted or when it has been refused several times because the function: Permission.storage.status always returns "denied".

****** EDIT ******

The problem arises when the user refuses the same permission several times (2 times) because the permissions request popup is no longer shown, in which case it is necessary to manually open the application settings and modify the permissions by hand. I have to make sure that: the first two times I request permissions with the popup then I should open the settings screen

Answer

Question answered by mrgnhnt96 (source).

I have always managed my permissions using two statuses granted and limited (used only for iOS14+). These two permissions are the only truethy statuses. All the others are falsey statuses.

the permission_handler package handles a lot of logic for you already. Before it makes the request, it will check the status to see if it is already defined. If it is, then it will return the status. If the permission has never been requested, then it will request the permission.

Personally, I set up a generic method for a permission request, to keep things DRY.

Future<bool> requestPermission(Permission setting) async {
  // setting.request() will return the status ALWAYS
  // if setting is already requested, it will return the status
  final _result = await setting.request();
  switch (_result) {
    case PermissionStatus.granted:
    case PermissionStatus.limited:
      return true;
    case PermissionStatus.denied:
    case PermissionStatus.restricted:
    case PermissionStatus.permanentlyDenied:
      return false;
  }
}

I then make a request like

final canUseStorage = await requestPermission(Permission.storage);

if (canUseStorage) {
  // do something with storage
}

If you have UI that is dependent on a status from Permission, then you still call Permission.storage.status.

[EDIT]
At the moment, you can't track how many times the request pop-up has been shown via permission_handler. It only returns the status. You would need to take the user to the settings depending on the returned status value.

Side Note
Instead of taking the user directly to the settings. Maybe you show a pop up saying "Looks like we don't have permission...", with a button that the user can tap to go to the settings, provides the user with some context as to why they need to go to their settings. And it's also a better user experience!

ANDROID ANDROID-PERMISSIONS FLUTTER
SHARE: