gpt4 book ai didi

android - ERROR_ALREADY_REQUESTING_PERMISSIONS flutter

转载 作者:IT王子 更新时间:2023-10-29 06:39:28 28 4
gpt4 key购买 nike

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

如何处理?谢谢你的回答

最佳答案

事情是当它检查权限时它返回 null 而不是任何权限状态,这会导致异常。这可能是一个错误,很多人已经解决了与位置权限相关的问题。

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();
}
}

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

关于android - ERROR_ALREADY_REQUESTING_PERMISSIONS flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56069917/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com