扑朔迷离ERROR_ALREADY_REQUESTING_PERMISSIONS [英] ERROR_ALREADY_REQUESTING_PERMISSIONS on flutter
问题描述
我创建了一个带有flutter的android应用程序,我在应用程序运行时第一次创建了权限请求,因此当用户单击拒绝"然后单击登录"按钮时,再次请求权限.我遇到了这个错误
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))
这是我的代码
@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
推荐答案
问题是,当检查权限时,它将返回null而不是任何Permission状态,这将导致异常.可能是一个错误,导致多个人填补了与位置权限相关的问题.
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.
这篇关于扑朔迷离ERROR_ALREADY_REQUESTING_PERMISSIONS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!