I'm working on a Flutter app where users need to grant photo access permission to load gallery images. I'm using Permission.photos for Android 33+ and Permission.storage for older versions. The gallery is managed with Bloc (GalleryBloc
).
Problem:
Even after granting permission, the GalleryBloc
does not reload the gallery images automatically. I have to restart the app for images to appear.
What I’ve Tried:
- Called
galleryBloc.add(LoadGallery());
after permission is granted. - Listened to the GalleryBloc stream to detect state changes.
- Verified
GalleryLoaded
state is emitted but the UI does not refresh. - Used
setState(() {});
insideGalleryLoaded
state to force UI rebuild. - Manually triggering
_onRefreshGallery
doesn’t always fetch the latest images.
Future<void> _requestPhotoPermission(BuildContext context) async {
PermissionStatus status;
try {
if (Platform.isAndroid) {
final deviceInfo = DeviceInfoPlugin();
final androidInfo = await deviceInfo.androidInfo;
final androidVersion = androidInfo.version.sdkInt;
status = (androidVersion >= 33)
? await Permission.photos.request()
: await Permission.storage.request();
} else {
status = await Permission.photos.request();
}
if (status.isGranted) {
setState(() {
_isgrated = true;
});
galleryBloc.add(LoadGallery()); //