I have created below method to check the location permission status in my flutter application:
Future<void> checkLocationPermission(
BuildContext context, UserCredential userCredential) async {
locationPermissionStatus = await permission_status.Permission.location
.request()
.then((value) async {
if (locationPermissionStatus.isGranted) {
displayToast("granted");
await getCurrentLocation(context, userCredential);
} else {
displayToast("not granted");
}
return locationPermissionStatus;
});
}
Where, PermissionStatus is declared as below:
permission_status.PermissionStatus locationPermissionStatus =
permission_status.PermissionStatus.denied;
Imports as below:
import 'package:location/location.dart';
import 'package:permission_handler/permission_handler.dart' as permission_status;
Used below plugins:
permission_handler: ^10.2.0
location: ^4.4.0
But In App, When Location Permission pops up, I am tapping on option "While Using the App"
But I am always getting the toast display as "not granted"
What might be the issue? Thanks in Advance.
EDIT: Already added below permissions for Android:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
</manifest>
CodePudding user response:
Adding to the @Romil Mavani answer, you first have to add permissions in AndroidManifest
:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
But it also seems that you are using async/await
with the combination of then
which is very error-prone. You should either use one or the other.
Future<void> checkLocationPermission(
BuildContext context, UserCredential userCredential) async {
locationPermissionStatus =
await permission_status.Permission.location.request();
if (locationPermissionStatus.isGranted) {
displayToast("granted");
await getCurrentLocation(context, userCredential);
} else {
displayToast("not granted");
}
}
CodePudding user response:
You have to add permission in manifest.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Like this : https://i.stack.imgur.com/PgdNF.png
Also permission status is not working correctly for only location permission so you have check and request permission using location package and it will works fine.
I Hope this things are solve your issue.
CodePudding user response:
If you're using a Windows Emulator
I've faced the same issue before, but when I opened the application setting
of my mobile's emulator to confirm, the location permission was not granted (off).
So, I granted it manually
from the app settings.
Emulator: Setting/apps/allApps/myApp/permisson
This solution works for me.