关于Flutter的错误已请求权限

x7rlezfr  于 2022-12-19  发布在  Flutter
关注(0)|答案(3)|浏览(202)

我创建了一个Android应用程序与flutter,我创建权限请求在第一次应用程序运行时,所以当用户点击拒绝,然后点击登录按钮,再次请求权限。我得到这个错误

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

如何处理?谢谢你的回答

icnyk63a

icnyk63a1#

问题是,当它检查权限时,它返回null而不是任何权限状态,这会导致异常。这可能是一个bug,多个人填写了与位置权限相关的问题。
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();
        }
      }

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

8ljdwjyq

8ljdwjyq2#

对于使用permission_handler更新版本的用户:

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

通过这种方式,您可以一次请求多个权限...

7gyucuyw

7gyucuyw3#

这对我很有效(这是为了联系许可,但对其他人也应该有效)

PermissionStatus permissionStatus; // note do not use PermissionStatus? permissionStatus;

while (true) {
    try {
       permissionStatus = await permission. Request();
       break; 
    } catch (e) {
       await Future.delayed(Duration(milliseconds: 500), () {});
    }
}

相关问题