Sub topic 4
Reading permission states
This chapter covers how to read a permission's current state and how to interpret it. It assumes you finished the runtime permission and platform declaration chapters. You should have geolocator and permission_handler in pubspec.yaml, with the AndroidManifest.xml and Info.plist entries in place. You should also be comfortable with async/await and with Dart 3 switch expressions.
Why a state is more than yes or no
A beginner's mental model of permissions is a boolean: the app has location or it doesn't. The real model has several states, and each one calls for a different reaction:
- The user has never been asked.
- The user said yes, but only while the app is open.
- The user said yes, always.
- The user said no, and the app may ask again.
- The user said no, and the system will not show the dialog again.
- A device policy blocks the permission, so the user cannot grant it even if they want to.
If you collapse these into granted and not granted, you will show a useless dialog in some cases and a confusing dead end in others. You read the state first, then decide what to do. Reading a state must never have side effects. Both calls below only report. Neither shows a dialog.
States in geolocator
Geolocator.checkPermission() returns a LocationPermission enum value:
| Value | Meaning | Can you read location? |
|---|---|---|
| `denied` | Not yet asked, or asked and refused, and you may ask again | No |
| `deniedForever` | The system will not show the dialog again | No |
| `whileInUse` | Granted while the app is in the foreground | Yes, in the foreground |
| `always` | Granted including when the app is in the background | Yes |
| `unableToDetermine` | The platform could not report a state, for example a browser without the Permissions API | Unknown |
denied covers two situations: the user has never been asked, and the user was asked and said no. The plugin does not separate them, because neither Android nor iOS gives an app a reliable "never asked" flag. Both situations lead to the same action, which is to call requestPermission(). You do not need to tell them apart.
States in permission_handler
Permission.locationWhenInUse.status returns a PermissionStatus:
| Value | Meaning |
|---|---|
| `denied` | Not granted, and you may ask |
| `granted` | Granted |
| `permanentlyDenied` | The dialog will not appear again. Only the Settings app can change this |
| `restricted` | iOS only. A policy such as Screen Time or an MDM profile forbids the permission, and the user cannot change it |
| `limited` | iOS only. Partial access. It is used for photos, not location |
| `provisional` | iOS only. Quiet provisional grant. It is used for notifications, not location |
For location you will meet denied, granted, permanentlyDenied and, on iOS, restricted. limited and provisional exist because the enum is shared by all permission types.
permission_handler uses different names for the same ideas, so the two packages line up like this:
| Situation | geolocator | permission_handler |
|---|---|---|
| May ask | `denied` | `denied` |
| Cannot ask again | `deniedForever` | `permanentlyDenied` |
| Foreground access | `whileInUse` | `granted` on `locationWhenInUse` |
| Background access | `always` | `granted` on `locationAlways` |
| Blocked by policy | `deniedForever` (iOS) | `restricted` |
Pick one package for the permission logic and stay with it. Mixing them in one flow makes the state tables above hard to keep in your head.
How the states change over time
States are not fixed once the user answers. The user can change them at any moment in system Settings, including while your app sits in the background.
Platform differences decide when the move to deniedForever happens:
- iOS: the dialog appears once. If the user taps "Don't Allow", the state becomes
deniedForeverimmediately from your app's point of view. Only Settings can change it. - Android 11 and later: the first refusal leaves you in
denied, so you can ask again. A second refusal makes the system stop showing the dialog, and the state becomesdeniedForever. - Android one-time grant: the dialog offers "Only this time". The app behaves as
whileInUse. When the app has been gone long enough, the grant lapses and the state returns todenied. Your code must not assume that a state seen at the previous launch still holds.
Android also needs a caveat about permanentlyDenied. Android does not expose "the system will stop asking" directly. Plugins infer it from the result of a request, so the permanent state can show up only after a request has returned. Treat the value returned by a request() call as the source of truth for that step, not a status read made just before it.
Reading states without prompting
The first example classifies the geolocator state into the decisions your UI cares about. The enum has five values and the switch expression is exhaustive, so the compiler warns you if a future version adds a value.
import 'package:geolocator/geolocator.dart';
enum LocationAccess { canRead, canAsk, mustUseSettings, unknown }
LocationAccess classify(LocationPermission permission) {
return switch (permission) {
LocationPermission.whileInUse ||
LocationPermission.always =>
LocationAccess.canRead,
LocationPermission.denied => LocationAccess.canAsk,
LocationPermission.deniedForever => LocationAccess.mustUseSettings,
LocationPermission.unableToDetermine => LocationAccess.unknown,
};
}
Future<void> main() async {
final permission = await Geolocator.checkPermission();
print('Raw state: $permission');
print('Decision: ${classify(permission)}');
}This function keeps platform vocabulary out of your widgets. A widget switches on LocationAccess, which you control, rather than on a plugin enum.
The same idea with permission_handler covers all six values:
import 'package:permission_handler/permission_handler.dart';
String describe(PermissionStatus status) {
return switch (status) {
PermissionStatus.granted => 'Location is available.',
PermissionStatus.denied => 'Location is off. We can ask.',
PermissionStatus.permanentlyDenied => 'Open Settings to enable location.',
PermissionStatus.restricted => 'A device policy blocks location.',
PermissionStatus.limited => 'Limited access.',
PermissionStatus.provisional => 'Provisional access.',
};
}
Future<void> printLocationStatus() async {
final status = await Permission.locationWhenInUse.status;
print(describe(status));
}Reading .status is a pure query. Calling .request() is what shows the dialog. Keep these two calls clearly separated in your code.
Granted does not mean working
A granted permission is necessary but not sufficient for reading a position. Two other conditions can still block you:
- The device-wide location switch can be off. That is a separate check, covered in the next chapter.
- On Android 12 and later the user can grant only approximate location. The state still reads as granted (
whileInUse), but positions are coarse. The accuracy chapter shows how to detect this.
So canRead in the example above means "the permission allows reading", not "a position will arrive".
Common mistakes
- Treating
deniedas final.deniedmeans you may still ask. Showing a "go to Settings" message here sends users out of the app when a normal dialog would have worked. - Treating
deniedForeveras retryable. CallingrequestPermission()in this state returns the same result and shows nothing. Your button appears broken. - Caching the state. A value read at startup can be stale by the time the user comes back from the background. Read it again where the decision is made.
- Asking for the wrong permission type.
Permission.locationandPermission.locationWhenInUseare not the same on iOS. Request the narrowest one your feature needs, and check the same one you request. - Ignoring
unableToDetermine. It is rare on mobile, but aswitchthat forgets it either fails to compile or falls into a default that hides real problems.
Checking your understanding
Exercise
A user installs your app on Android 12 and taps "Don't allow" on the location dialog. Your code calls Geolocator.checkPermission() right afterward. Which value do you expect, and should you show a "Open Settings" button?
Show answer
Expect LocationPermission.denied. On Android 11 and later the first refusal does not make the denial permanent, so you may still call requestPermission() again. Do not show the Settings button yet. A second refusal makes the system stop showing the dialog, and the state then becomes deniedForever. That is when you send the user to Settings.
Exercise
On iOS, Permission.locationWhenInUse.status returns PermissionStatus.restricted. Why is calling openAppSettings() the wrong response, and what should the app do instead?
Show answer
restricted means a policy such as Screen Time or an MDM profile blocks the permission, so the user cannot grant it, even in Settings. Opening Settings leads to a dead end. The app should explain that a device policy blocks location and offer a fallback, for example typing an address by hand.
Exercise
Why is it useful to wrap LocationPermission in your own enum such as LocationAccess before using it in widgets?
Show answer
It keeps platform and plugin naming out of the UI. Widgets decide based on three or four outcomes you define (can read, can ask, must use Settings, unknown). If you switch plugins or a plugin renames a value, only the one mapping function changes. An exhaustive switch in that function also makes the compiler flag newly added enum values.