Sub topic 6
Building the Check-Then-Request Flow
This chapter assumes you know basic Dart and Flutter, including async/await, Future, and adding packages to pubspec.yaml. It also assumes that Android and iOS declarations have already been added. The examples use the geolocator package.
Why the order matters
Location access has two separate gates:
- The device location service must be enabled.
- Your app must have permission to use location.
A user can enable the device service but deny your app. The user can also grant your app permission while the device service is turned off. Check both conditions before reading a position.
Permission is requested at runtime. Android and iOS show the system dialog, and the user controls the result. Your app should not repeatedly request permission after a denial. First explain why the feature needs location, then ask at the point where the user starts that feature.
Implementing the flow
checkPermission() reads the current state without showing a dialog. Call requestPermission() only when the state is denied. After the request, check the result again.
import 'package:geolocator/geolocator.dart';
Future<Position> determinePosition() async {
final serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return Future.error('Location services are disabled.');
}
var permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error('Location permission was denied.');
}
}
if (permission == LocationPermission.deniedForever) {
return Future.error('Location permission is permanently denied.');
}
return Geolocator.getCurrentPosition(
locationSettings: const LocationSettings(
accuracy: LocationAccuracy.high,
),
);
}The function stops early when the service is disabled. It then checks permission and requests it only for the ordinary denied state. If the user grants access, the function reads one Position.
A permanently denied state means that another normal request is unlikely to show a useful dialog. The user must usually change the permission in the operating system settings. Treat this as a separate UI state rather than as an ordinary error.
Designing the user experience
A rationale is a short explanation shown before the system dialog. It should describe the user-visible feature, such as showing nearby places or recording a route. Do not request location as soon as the app starts unless the app cannot function without it.
Keep the outcomes distinct:
| Condition | Appropriate response |
|---|---|
| Service disabled | Tell the user to enable device location |
| Permission denied | Explain the feature and allow a retry |
| Permanently denied | Offer a link to app settings |
| Permission granted | Continue to the location feature |
When a user declines, provide a useful alternative where possible. For example, allow manual map search instead of blocking the entire screen. This respects the choice and makes the permission request easier to understand.
Exercise
Why should an app check isLocationServiceEnabled() separately from checkPermission()?
Show answer
They represent different controls. The device service can be off even when the app has permission, and the app can lack permission even when the service is on.