I have the following problem: I have a flutter application which uses Geolocator to determine users location. The application works perfectly well with Precise location, but when choosing Approximate location it causes an error: "java.lang.SecurityException: Neither user 10166 nor current process has android.permission.ACCESS_FINE_LOCATION." then the app closes by itself.
When I go to android emulator device settings and change location permissions from "Ask every time" to "Allow only while using the app" the problem does not reproduce, but that is not the behavior I would want the users to experience – I want them to see the Android dialog which asks them If they allow their location to be used and to choose between Precise and Approximate location.
Do you have any ideas? I use flutter 3.19.6 and geolocator 12.0.0
I tried several things, for example adding android.permission.ACCESS_FINE_LOCATION and android.permission.ACCESS_COARSE_LOCATION to AndroidManifest files, but this did not help.
Moreover I requested for permissions at runtime, but this did not fix the problem. That is the code:
Future<void> getCurrrentUserLocation() async {
bool serviceEnabled;
LocationPermission permission;
// Check if location services are enabled.
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
print('Location services are disabled.');
return;
}
// Check the current permission status.
//permission = await Geolocator.requestPermission();
permission = await Geolocator.checkPermission();
print('Initial permission status: $permission');
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
print('Permission after request: $permission');
if (permission == LocationPermission.denied) {
print('Location permissions are denied.');
return;
}
}
if (permission == LocationPermission.deniedForever) {
print('Location permissions are permanently denied, we cannot request permissions.');
return;
}
if (permission == LocationPermission.whileInUse || permission == LocationPermission.always) {
try {
Position position = await Geolocator.getCurrentPosition(timeLimit: const Duration(seconds: 15));
print('Latitude: ${position.latitude}, Longitude: ${position.longitude}');
} catch (e) {
print('Error occurred while trying to get location: $e');
}
} else {
print('Location permissions are not granted.');
}
2
Answers
The problem comes from last geolocator version 12.0.0. A bug and fix with version downgrade is described here: https://github.com/Baseflow/flutter-geolocator/issues/1515
In your android/app/build.gradle file, make sure that the compileVersion is equal to
Also, add the following permissions to your AndroidManifest.xml: