ub3lal hace 1 mes
padre
commit
d328d1fc04
Se han modificado 1 ficheros con 23 adiciones y 4 borrados
  1. 23 4
      lib/src/bloc/cashed_image_bloc.dart

+ 23 - 4
lib/src/bloc/cashed_image_bloc.dart

@@ -10,6 +10,8 @@ part 'cashed_image_state.dart';
 
 class CashedImageBloc extends Bloc<CashedImageEvent, CashedImageState> {
   DefaultCacheManager defaultCacheManager = DefaultCacheManager();
+
+  Map<String, Uint8List> mapcahed = <String, Uint8List>{};
   CashedImageBloc() : super(CashedImageInitial()) {
     on<GetStartImageEvent>((event, emit) async {
       emit(CashedImageLoadingState());
@@ -20,14 +22,29 @@ class CashedImageBloc extends Bloc<CashedImageEvent, CashedImageState> {
       );
       if (event.cached) {
         FileInfo? fileInfo;
+        Uint8List? bytes;
         if (event.cachkey != null) {
-          fileInfo = await defaultCacheManager.getFileFromCache(event.cachkey!);
+          if (mapcahed[event.cachkey!] == null) {
+            fileInfo = await defaultCacheManager.getFileFromCache(
+              event.cachkey!,
+            );
+            if (fileInfo != null) {
+              mapcahed[event.cachkey!] = await fileInfo.file.readAsBytes();
+            }
+          }
+          bytes = mapcahed[event.cachkey!];
         } else {
-          fileInfo = await defaultCacheManager.getFileFromCache(event.url);
+          if (mapcahed[event.url] == null) {
+            fileInfo = await defaultCacheManager.getFileFromCache(event.url);
+            if (fileInfo != null) {
+              mapcahed[event.url] = await fileInfo.file.readAsBytes();
+            }
+          }
+          bytes = mapcahed[event.url];
         }
 
-        if (fileInfo != null) {
-          emit(CashedImageGetState(bytes: await fileInfo.file.readAsBytes()));
+        if (bytes != null) {
+          emit(CashedImageGetState(bytes: bytes));
           return;
         }
       }
@@ -37,8 +54,10 @@ class CashedImageBloc extends Bloc<CashedImageEvent, CashedImageState> {
         if (event.cached) {
           if (event.cachkey != null) {
             defaultCacheManager.putFile(event.cachkey!, uint8list);
+            mapcahed[event.cachkey!] = uint8list;
           } else {
             defaultCacheManager.putFile(event.url, uint8list);
+            mapcahed[event.url] = uint8list;
           }
         }