Phot library permission is not working as expected for SDK 32 and lower and resulting in permission denied every time or asking for duplicate permission. Also, if user has permanently denied permission, we should check that and handle it properly. If that is the case, you may create work around like this.
import 'package:permission_handler/permission_handler.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'dart:io' show Platform;
Future<List<dynamic>> runTimePermission() async {
// Check if running on the web platform
if (kIsWeb) {
return [true, 'Permission granted automatically on web platform.'];
}
try {
// Determine the appropriate permission based on platform and SDK version
Permission permission;
if (Platform.isAndroid) {
final androidInfo = await DeviceInfoPlugin().androidInfo;
final bool isAndroid13OrHigher = androidInfo.version.sdkInt >= 33;
permission = isAndroid13OrHigher ? Permission.photos : Permission.storage;
} else if (Platform.isIOS) {
permission = Permission.photos; // Use photos permission for iOS
} else {
// For any unsupported platforms (just in case)
return [false, 'Unsupported platform for this permission request.'];
}
// Get the current status of the selected permission
var status = await permission.status;
if (status.isGranted) {
return [true, 'Permission has already been granted.'];
} else if (status.isDenied || status.isLimited) {
// Request permission if initially denied or limited
var result = await permission.request();
if (result.isGranted) {
return [true, 'Permission granted after request.'];
} else if (result.isPermanentlyDenied) {
return [
false,
'Permission permanently denied. Please enable it in settings to continue.'
];
} else {
return [false, 'Permission denied. Unable to proceed without access.'];
}
} else if (status.isRestricted) {
return [false, 'Permission is restricted and cannot be granted.'];
} else if (status.isPermanentlyDenied) {
return [
false,
'Permission permanently denied. Please enable it in settings to use this feature.'
];
}
} catch (e) {
// Return a general error message in case of an exception
return [
false,
'An error occurred while checking/requesting permission: $e'
];
}
// Default case if no conditions are met
return [false, 'Permission request failed unexpectedly.'];
}