ensurePermissionOrOpenSettings function
Ensures a permission is granted through a comprehensive flow.
This is a convenience method that implements a complete permission handling strategy:
- First checks if the permission is already granted
- If not granted, requests the permission from the user
- If the request is denied, opens the device settings for manual configuration
This method provides the most user-friendly approach to permission handling by automatically escalating through the available options.
Parameters:
p: The KruzrPermission to ensure is granted.
Returns: A Future<bool> that resolves to:
trueif the permission is granted (either initially or after request)falseif the permission was not granted and settings were opened
Flow diagram:
Check Permission
↓
Granted? → Yes → Return true
↓ No
Request Permission
↓
Granted? → Yes → Return true
↓ No
Open Settings
↓
Return false
Note:
- When this method returns
false, the settings page has been opened but the permission status is still denied - Consider checking the permission again when the app regains focus in case the user granted it in settings
Example:
bool hasPermission = await ensurePermissionOrOpenSettings(KruzrPermission.location);
if (hasPermission) {
// Permission is granted, proceed with feature
startLocationTracking();
} else {
// Settings were opened, consider checking again when app regains focus
showMessage('Please grant location permission in settings and restart the feature.');
}
Implementation
Future<bool> ensurePermissionOrOpenSettings(KruzrPermission p) async {
final has = await checkPermission(p);
if (has) return true;
final after = await requestPermission(p);
if (after) return true;
await openSettings();
return false;
}