扑朔迷离ERROR_ALREADY_REQUESTING_PERMISSIONS

时间:2019-05-10 02:29:01

标签: android flutter android-permissions

我使用flutter创建了一个Android应用程序,我在应用程序运行时首次创建了权限请求,因此当用户单击“拒绝”然后单击“登录”按钮时,将再次请求权限。我收到此错误

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))

这是我的代码

@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
   }
}

如何处理?谢谢你的回答

2 个答案:

答案 0 :(得分:1)

问题是当它检查权限时,它返回null而不是任何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) {
        } else if (permission == PermissionStatus.denied ||
            permission == PermissionStatus.disabled ||
            permission == PermissionStatus.restricted) {
          await PermissionHandler().requestPermissions([PermissionGroup.storage]);
          getPermissionStatus();
        }
      }

我认为您的代码没有问题,您可以使用递归而不是创建两个单独的函数。

答案 1 :(得分:0)

对于那些使用更新版本的权限处理程序的人:

Future<void> permission() async {
  if (await Permission.storage.isDenied) {
    await Permission.storage.request();
  }
  if (await Permission.location.isDenied) {
    await Permission.location.request();
  }

这样就可以一次请求多个权限...