Sub topic 4
Reading Permission States
Prerequisites
This chapter assumes that you know basic Dart and Flutter, can use async and await, and have added the geolocator package to your Flutter project. It also assumes that the Android and iOS permission declarations already exist.
A permission request is not a single yes-or-no operation. The operating system reports a state that describes what the user has allowed and what your app may do next.
The states returned by `geolocator`
The geolocator package represents permission with the LocationPermission enum:
| State | Meaning | Usual next step |
|---|---|---|
| `denied` | The app does not currently have permission, but a request may still be possible. | Call `requestPermission()`. |
| `deniedForever` | The user has blocked permission permanently for the app. | Explain the problem and offer app settings. |
| `whileInUse` | The app may use location while it is visible or active. | Read the position or start a foreground stream. |
| `always` | The app may use location in the background, if the platform and declarations allow it. | Apply additional background-location rules. |
| `unableToDetermine` | The package cannot determine the current state. | Treat it as an error and avoid using location. |
The exact transitions depend on the platform and its version. For example, Android may let a user provide approximate location instead of precise location. That choice is separate from whether the user granted access.
Reading the current state
Use checkPermission() when your app starts a location-dependent action. This call reads the current state without showing a dialog.
import 'package:geolocator/geolocator.dart';
Future<String> describeLocationPermission() async {
final permission = await Geolocator.checkPermission();
switch (permission) {
case LocationPermission.denied:
return 'Location has not been granted yet.';
case LocationPermission.deniedForever:
return 'Location is blocked in system settings.';
case LocationPermission.whileInUse:
return 'Location is available while the app is in use.';
case LocationPermission.always:
return 'Location is available in the background.';
case LocationPermission.unableToDetermine:
return 'The location permission state is unknown.';
}
}This function does not check whether the device's location service is enabled. A user can grant your app permission while the device-wide location switch remains off. Check those conditions separately.
Do not treat deniedForever as an ordinary denial. Repeatedly calling requestPermission() usually cannot display a useful prompt after permanent denial. The user must change the app's permission in system settings.
Model the decision as a state flow
Your UI can map each state to a deliberate action instead of scattering permission checks across widgets.
Keep state handling explicit
A common error is checking only permission == LocationPermission.denied and then continuing for every other value. That accidentally treats deniedForever or unableToDetermine as usable access.
A safer rule is:
- Continue only for
whileInUseoralways. - Request permission for
denied. - Send the user toward settings for
deniedForever. - Stop and report an error for
unableToDetermine.
Recheck the state when the app returns from settings or resumes after being paused. The user may have changed the permission while your Flutter process was not active.
Exercise
What should your app do when checkPermission() returns deniedForever?
Show answer
Stop the location operation, explain that permission is blocked, and offer a link to the app's system settings. Do not rely on another permission request to repair the state.