Reputation: 1
First of all the device I am using to run this app is running android 13.
So for this gelocator package as per the example in the package page I have added it according to my needs to this function,
void getLocation() async{
bool serviceEnabled;
LocationPermission permission;
// Test if location services are enabled.
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
// Location services are not enabled don't continue
// accessing the position and request users of the
// App to enable the location services.
return Future.error('Location services are disabled.');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
// Permissions are denied, next time you could try
// requesting permissions again (this is also where
// Android's shouldShowRequestPermissionRationale
// returned true. According to Android guidelines
// your App should show an explanatory UI now.
return Future.error('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately.
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
}
final LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.low,
distanceFilter: 100,
);
Position position = await Geolocator
.getCurrentPosition(locationSettings: locationSettings);
print(position);
}
In this, if I remove these parts,
return Future.error('Location services are disabled.');
return Future.error('Location permissions are denied');
the app asks for permission everytime, if it is denied and also the OS asks to turn on location services, if it is not turned on.
But if I keep it as it is as shown in the above code, it only asks for one time if the location permission is denied and if the location services are OFF the OS does not ask for it to be turned on at all. And then it just prints the exception in the console instead of asking for permission if it is denied.
It is more convenient for it to just ask for permission rather than printing in the console as unhandled exception and there is no need for me to handle that exception when those error return statements are not there.
This is not a problem. I am just asking to know if there any exception handling occuring in the background or am I missing some knowledge here?
Thanks in advance! Also sorry for my bad english.
Upvotes: 0
Views: 39
Reputation: 112
void getLocation() async{
bool serviceEnabled;
LocationPermission permission;
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
// One more thing you can do is open App setting
// TODO(User): Open App Setting
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
}
final LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.low,
distanceFilter: 100,
);
Position position = await Geolocator
.getCurrentPosition(locationSettings: locationSettings);
print(position);
}
Please check the refactored code because according to your logic, the isServiceEnable value will be false so you return an error and execution of a further program is stopped.
So, first, you check the permission using the checkPermission() method and based on the checkPermission() function status request the location permission. I assume that you have provided the permission in the manifest file as per the documentation.
Upvotes: 0