Sub topic 2
Location Services Off Versus Permission Denied
This chapter assumes you know Dart async/await and Futures, have basic Flutter widget knowledge, can add packages with pubspec.yaml, and can edit the android and ios folders of a project.
Location can fail in two separate ways. The device-wide location service may be off, or the app may lack permission. Each needs a different fix, so check them in order.
| Check | Call (geolocator) | If false |
|---|---|---|
| Device location service on | `Geolocator.isLocationServiceEnabled()` | Ask the user to turn on device location. Requesting permission will not help. |
| App permission granted | `Geolocator.checkPermission()` | Request permission, or explain why you need it. |
Building the check-then-request flow
Check the service first, then check the permission, and request it only when the status is denied.
import 'package:geolocator/geolocator.dart';
Future<Position> determinePosition() async {
if (!await Geolocator.isLocationServiceEnabled()) {
throw Exception('Location services are off.');
}
var permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
}
if (permission == LocationPermission.denied) {
throw Exception('Location permission was denied.');
}
if (permission == LocationPermission.deniedForever) {
throw Exception('Location permission is permanently denied.');
}
return Geolocator.getCurrentPosition(
locationSettings: const LocationSettings(accuracy: LocationAccuracy.high),
);
}Explaining why you need location
Show a short rationale before calling requestPermission(), such as "We use your location to show nearby stores." Android and iOS show the system dialog only at a set point, and a user who denies it has limited options afterward. The iOS usage description in Info.plist appears in that dialog, so write it to match your rationale.
Handling permanent denial and sending users to settings
After a permanent denial, the system dialog will not appear again. The only option is to send the user to the app settings page.
import 'package:permission_handler/permission_handler.dart';
Future<void> handlePermanentDenial() async {
final status = await Permission.locationWhenInUse.status;
if (status.isPermanentlyDenied) {
await openAppSettings();
}
}Coarse, fine, and approximate location
Precision depends on what the user and the platform allow.
| Setting | Android | iOS | Typical use |
|---|---|---|---|
| Coarse or approximate | `ACCESS_COARSE_LOCATION` | Approximate location toggle | City-level results |
| Fine or precise | `ACCESS_FINE_LOCATION` | Precise location toggle | Turn-by-turn or nearby results |
Request LocationAccuracy.high only when you need it. Use a lower setting when the user has granted approximate location, and handle that case in your UI.
Exercise
A user denied location permission twice on Android, and the system dialog no longer appears. Which call should you make, and why does the check-then-request flow alone not fix this?
Show answer
Call openAppSettings() from permission_handler. The check-then-request flow only handles denied, which allows another request. A deniedForever or permanently denied status blocks the system dialog, so the user must change the permission in app settings.