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
Setup. While the permissions are being requested during runtime, you'll still need to tell the OS which permissions your app might potentially use. That requires adding permission configuration to Android- and iOS-specific files. Since version 4.4.0 this plugin is implemented using the Flutter 1.12 Android plugin APIs.
We also need to add capabilities to our app, so that the location request can be granted by the operating system and the user. For Android, go to android/app/src/main/AndroidManifest. xml and add the capability <uses-permission android:name="android. permission.
Android. Add the permissions your app needs to the android/app/src/main/AndroidManifest. xml. Put the permissions in the manifest tag, infront of the application tag.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With