Sub topic 8
Handling Permanent Denial and Sending Users to Settings
Assume you know basic Dart, Flutter widgets, async/await, and how to add packages to pubspec.yaml. This chapter uses geolocator for location access and permission_handler for opening the app's settings page.
A permission request can end in several states:
denied: the user has not granted access. The app may be able to ask again.deniedForever: the operating system will not show the permission dialog again.granted: the app can use the requested location access.- Restricted or limited states may also occur on some platforms or configurations.
Permanent denial is different from an ordinary denial. Calling requestPermission() repeatedly will not solve it. The user must change the permission in the operating system settings.
Detect permanent denial
With geolocator, check the result of checkPermission() or requestPermission():
import 'package:geolocator/geolocator.dart';
Future<bool> ensureLocationPermission() async {
var permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
}
if (permission == LocationPermission.deniedForever) {
return false;
}
return permission == LocationPermission.always ||
permission == LocationPermission.whileInUse;
}This function returns false for permanent denial. The user interface should then explain what happened and offer a settings action. Do not silently fail or keep retrying the request.
With permission_handler, the equivalent check uses PermissionStatus:
import 'package:permission_handler/permission_handler.dart';
Future<bool> ensureLocationPermission() async {
final status = await Permission.locationWhenInUse.request();
if (status.isGranted) {
return true;
}
if (status.isPermanentlyDenied) {
await openAppSettings();
}
return false;
}openAppSettings() sends the user to the settings page for your application. The user can manually enable location access there. The function returns whether the system could open the settings page, so production code can show an additional message if it fails.
Use a respectful recovery flow
A good recovery flow has these steps:
- Explain why the app needs location before requesting it.
- Request permission once when the feature needs it.
- If the user denies it, keep the feature unavailable and offer a retry later.
- If the user permanently denies it, explain that the change must happen in Settings.
- Open settings only after the user taps a clear action such as Open Settings.
- Check the permission again when the user returns to the app.
The app should not treat permanent denial as an error that requires immediate repeated prompts. The user's choice is valid. Provide a useful alternative, such as entering a city manually or browsing without nearby results.
Recheck after returning from settings
Settings changes happen outside the Flutter process. A permission value stored before navigation can therefore be stale. Recheck it when the app resumes.
A widget can observe lifecycle changes with WidgetsBindingObserver:
class LocationPageState extends State<LocationPage>
with WidgetsBindingObserver {
bool locationEnabled = false;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_refreshPermission();
}
}
Future<void> _refreshPermission() async {
final permission = await Geolocator.checkPermission();
if (!mounted) return;
setState(() {
locationEnabled =
permission == LocationPermission.always ||
permission == LocationPermission.whileInUse;
});
}
}The mounted check prevents setState from running after the page has been removed.
Exercise
Why should an app check location permission again after the user returns from application settings?
Show answer
The user may have changed the permission outside the app, so the value previously held in memory may be stale.