requestPhotoPermissions method
Requests photo library/gallery permission.
Platform-specific behavior:
- iOS: Checks for photos or photosAddOnly permission. Requests full photos access if not granted.
- Android: Always returns
true(storage permissions handled differently on Android 10+)
Returns true if permission is granted or not required (Android),
false if the user denies permission on iOS.
Example:
final permissionService = PermissionService();
if (await permissionService.requestPhotoPermissions()) {
// Open gallery
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
} else {
// Show error message (iOS only)
showDialog('Photo library access is required');
}
Note: Ensure you have added photo permissions to iOS Info.plist:
NSPhotoLibraryUsageDescription- For reading photosNSPhotoLibraryAddUsageDescription- For adding photos
On Android 10+ (API 29+), scoped storage is used and no explicit permission is needed for gallery access.
Implementation
Future<bool> requestPhotoPermissions() async {
if (Platform.isIOS) {
if (await Permission.photos.status.isGranted ||
await Permission.photosAddOnly.status.isGranted) {
return true;
} else {
var photosStatus = await Permission.photos.request();
return photosStatus.isGranted;
}
} else {
return true;
}
}