Sub topic 5
Location Services Off Versus Permission Denied
Prerequisites
This chapter assumes that you know basic Dart and Flutter widget development. You should also be comfortable with async and await, Futures, packages in pubspec.yaml, and editing the Android and iOS folders of a Flutter project.
A location request has two independent gates:
- The device's location service must be enabled.
- Your app must have permission to use location.
The first gate belongs to the device. The second gate belongs to your app and the user’s choice.
Two different problems
A user can grant your app location permission while turning off the device-wide location service. The reverse can also happen: the service can be enabled while your app has no permission.
| Condition | Meaning | Appropriate recovery |
|---|---|---|
| Service disabled | The device is not providing location to apps | Ask the user to enable Location in device settings |
| Permission denied | The app is not allowed to use location | Explain the feature and request permission |
| Permission denied forever | The app cannot show the normal permission dialog again | Offer a link to app settings |
| Service enabled and permission granted | The app can try to read location | Read a position or start a stream |
These conditions are not interchangeable. Calling requestPermission() cannot turn on the device service. Sending a user to app settings does not necessarily enable the device service.
Check the service before permission
Check the service first. There is no benefit in asking for app permission when the device cannot provide location at all.
The following function uses geolocator and returns a value that the UI can handle without guessing what went wrong.
import 'package:geolocator/geolocator.dart';
enum LocationReadiness {
serviceDisabled,
permissionDenied,
permissionDeniedForever,
ready,
}
Future<LocationReadiness> checkLocationReadiness({
bool requestPermission = false,
}) async {
final serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return LocationReadiness.serviceDisabled;
}
var permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied && requestPermission) {
permission = await Geolocator.requestPermission();
}
switch (permission) {
case LocationPermission.denied:
return LocationReadiness.permissionDenied;
case LocationPermission.deniedForever:
return LocationReadiness.permissionDeniedForever;
case LocationPermission.always:
case LocationPermission.whileInUse:
return LocationReadiness.ready;
case LocationPermission.unableToDetermine:
return LocationReadiness.permissionDenied;
}
}Pass requestPermission: true when the user has deliberately started a feature that needs location. For a passive status check, leave it false. This prevents a permission dialog from appearing unexpectedly when a screen opens.
React to each result
The UI should explain the action the user can take:
- For
serviceDisabled, show that device Location is off. A button can open the device's location settings, or the user can enable it manually. - For
permissionDenied, show a short explanation and let the user try again. - For
permissionDeniedForever, explain that the app cannot show the normal dialog again. Offer an app-settings button. - For
ready, callgetCurrentPosition()or start a position stream.
Recheck after settings changes
Users may leave your app, enable Location, or change the app permission, and then return. Check the status again when the app resumes instead of assuming the old result is still valid.
A WidgetsBindingObserver can notify a stateful widget when the app returns to the foreground. Keep the check separate from the widget so the same logic can be tested without rendering a screen.
Exercise
Exercise
The device Location switch is off, but the app previously received location permission. Should the app call requestPermission() again?
Show answer
No. Permission and the device service are separate gates. Explain that Location is disabled and ask the user to enable the device service, then recheck both conditions.