Skip to content

Commit 7eea0ed

Browse files
committed
add onError which allows the user to supply their own response to firebase storage errors
1 parent 8993075 commit 7eea0ed

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

lib/src/firebase_image.dart

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import 'package:firebase_storage/firebase_storage.dart';
99
import 'package:flutter/foundation.dart';
1010
import 'package:flutter/material.dart';
1111

12+
import 'cache_manager.dart';
1213
import 'image_fetch_strategy.dart';
1314

15+
typedef FirebaseImageError = Uint8List Function(Exception exception);
16+
1417
class 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() {
@@ -79,29 +92,34 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
7992

8093
Future<Uint8List> _fetchImage() async {
8194
Uint8List bytes;
82-
FirebaseImageCacheManager cacheManager = FirebaseImageCacheManager(
83-
cacheRefreshStrategy: cacheRefreshStrategy,
84-
imageFetchStrategy: imageFetchStrategy,
85-
);
8695

87-
if (shouldCache) {
88-
await cacheManager.open();
89-
FirebaseImageObject localObject =
90-
await cacheManager.get(_imageObject.uri, this);
96+
try {
97+
if (shouldCache) {
98+
await _cacheManager.open();
99+
FirebaseImageObject localObject =
100+
await _cacheManager.get(_imageObject.uri, this);
91101

92-
if (localObject != null) {
93-
bytes = await cacheManager.localFileBytes(localObject);
94-
if (bytes == null) {
95-
bytes = await cacheManager.upsertRemoteFileToCache(
102+
if (localObject != null) {
103+
bytes = await _cacheManager.localFileBytes(localObject);
104+
if (bytes == null) {
105+
bytes = await _cacheManager.upsertRemoteFileToCache(
96106
_imageObject, this.maxSizeBytes);
107+
}
108+
} else {
109+
bytes = await _cacheManager.upsertRemoteFileToCache(
110+
_imageObject, this.maxSizeBytes);
97111
}
98112
} else {
99-
bytes = await cacheManager.upsertRemoteFileToCache(
100-
_imageObject, this.maxSizeBytes);
113+
bytes =
114+
await _cacheManager.remoteFileBytes(_imageObject, this.maxSizeBytes);
115+
}
116+
}
117+
catch (ex) {
118+
await delete();
119+
120+
if (this.onError != null) {
121+
bytes = this.onError(ex);
101122
}
102-
} else {
103-
bytes =
104-
await cacheManager.remoteFileBytes(_imageObject, this.maxSizeBytes);
105123
}
106124

107125
return bytes;
@@ -112,6 +130,13 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
112130
.instantiateImageCodec(await _fetchImage());
113131
}
114132

133+
Future<bool> delete() async {
134+
await _cacheManager.open();
135+
await _cacheManager.delete(_imageObject.uri);
136+
137+
return super.evict();
138+
}
139+
115140
@override
116141
Future<FirebaseImage> obtainKey(ImageConfiguration configuration) {
117142
return SynchronousFuture<FirebaseImage>(this);

0 commit comments

Comments
 (0)