@@ -9,8 +9,11 @@ import 'package:firebase_storage/firebase_storage.dart';
99import 'package:flutter/foundation.dart' ;
1010import 'package:flutter/material.dart' ;
1111
12+ import 'cache_manager.dart' ;
1213import 'image_fetch_strategy.dart' ;
1314
15+ typedef FirebaseImageError = Uint8List Function (Exception exception);
16+
1417class FirebaseImage extends ImageProvider <FirebaseImage > {
1518 // Default: True. Specified whether or not an image should be cached (optional)
1619 final bool shouldCache;
@@ -33,6 +36,11 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
3336 /// Default: FETCH_TO_MEMORY. Specifies the strategy in which to fetch the image from Firebase (optional)
3437 final ImageFetchStrategy imageFetchStrategy;
3538
39+ /// An optional response to when an error occurs (optional)
40+ final FirebaseImageError onError;
41+
42+ final FirebaseImageCacheManager _cacheManager;
43+
3644 /// Fetches, saves and returns an ImageProvider for any image in a readable Firebase Cloud Storeage bucket.
3745 ///
3846 /// [location] The URI of the image, in the bucket, to be displayed
@@ -49,11 +57,16 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
4957 this .cacheRefreshStrategy = CacheRefreshStrategy .BY_METADATA_DATE ,
5058 this .firebaseApp,
5159 this .imageFetchStrategy = ImageFetchStrategy .FETCH_TO_MEMORY ,
60+ this .onError,
5261 }) : _imageObject = FirebaseImageObject (
5362 bucket: _getBucket (location),
5463 remotePath: _getImagePath (location),
5564 reference: _getImageRef (location, firebaseApp),
56- );
65+ ),
66+ _cacheManager = FirebaseImageCacheManager (
67+ cacheRefreshStrategy: cacheRefreshStrategy,
68+ imageFetchStrategy: imageFetchStrategy,
69+ );
5770
5871 /// Returns the image as bytes
5972 Future <Uint8List > getBytes () {
@@ -78,29 +91,34 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
7891
7992 Future <Uint8List > _fetchImage () async {
8093 Uint8List bytes;
81- FirebaseImageCacheManager cacheManager = FirebaseImageCacheManager (
82- cacheRefreshStrategy: cacheRefreshStrategy,
83- imageFetchStrategy: imageFetchStrategy,
84- );
8594
86- if (shouldCache) {
87- await cacheManager.open ();
88- FirebaseImageObject localObject =
89- await cacheManager.get (_imageObject.uri, this );
95+ try {
96+ if (shouldCache) {
97+ await _cacheManager.open ();
98+ FirebaseImageObject localObject =
99+ await _cacheManager.get (_imageObject.uri, this );
90100
91- if (localObject != null ) {
92- bytes = await cacheManager .localFileBytes (localObject);
93- if (bytes == null ) {
94- bytes = await cacheManager .upsertRemoteFileToCache (
101+ if (localObject != null ) {
102+ bytes = await _cacheManager .localFileBytes (localObject);
103+ if (bytes == null ) {
104+ bytes = await _cacheManager .upsertRemoteFileToCache (
95105 _imageObject, this .maxSizeBytes);
106+ }
107+ } else {
108+ bytes = await _cacheManager.upsertRemoteFileToCache (
109+ _imageObject, this .maxSizeBytes);
96110 }
97111 } else {
98- bytes = await cacheManager.upsertRemoteFileToCache (
99- _imageObject, this .maxSizeBytes);
112+ bytes =
113+ await _cacheManager.remoteFileBytes (_imageObject, this .maxSizeBytes);
114+ }
115+ }
116+ catch (ex) {
117+ await delete ();
118+
119+ if (this .onError != null ) {
120+ bytes = this .onError (ex);
100121 }
101- } else {
102- bytes =
103- await cacheManager.remoteFileBytes (_imageObject, this .maxSizeBytes);
104122 }
105123
106124 return bytes;
@@ -111,6 +129,13 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
111129 .instantiateImageCodec (await _fetchImage ());
112130 }
113131
132+ Future <bool > delete () async {
133+ await _cacheManager.open ();
134+ await _cacheManager.delete (_imageObject.uri);
135+
136+ return super .evict ();
137+ }
138+
114139 @override
115140 Future <FirebaseImage > obtainKey (ImageConfiguration configuration) {
116141 return SynchronousFuture <FirebaseImage >(this );
0 commit comments