ERROR_ALREADY_REQUESTING_PERMISSIONS on flutter

风格不统一 提交于 2020-08-06 11:49:03

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/56069917/error-already-requesting-permissions-on-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!