@@ -70,37 +70,54 @@ class AssetEntityImageProvider extends ImageProvider<AssetEntityImageProvider> {
70
70
AssetEntityImageProvider key,
71
71
DecoderCallback decode,
72
72
) async {
73
- assert (key == this );
74
- Uint8List ? data;
75
- if (isOriginal) {
73
+ try {
74
+ assert (key == this );
76
75
if (key.entity.type == AssetType .audio ||
77
76
key.entity.type == AssetType .other) {
78
77
throw UnsupportedError (
79
78
'Image data for the ${key .entity .type } is not supported.' ,
80
79
);
81
80
}
82
- if (key.entity.type == AssetType .video) {
81
+ Uint8List ? data;
82
+ final ImageFileType _type;
83
+ if (key.imageFileType == ImageFileType .other) {
84
+ // Assume the title is invalid here, try again with the async getter.
85
+ _type = _getType (await key.entity.titleAsync);
86
+ } else {
87
+ _type = key.imageFileType;
88
+ }
89
+ if (isOriginal) {
90
+ if (key.entity.type == AssetType .video) {
91
+ data = await key.entity.thumbDataWithOption (
92
+ _thumbOption (
93
+ Constants .defaultGridThumbSize,
94
+ Constants .defaultGridThumbSize,
95
+ ),
96
+ );
97
+ } else if (_type == ImageFileType .heic) {
98
+ data = await (await key.entity.file)? .readAsBytes ();
99
+ } else {
100
+ data = await key.entity.originBytes;
101
+ }
102
+ } else {
103
+ final List <int > _thumbSize = thumbSize! ;
83
104
data = await key.entity.thumbDataWithOption (
84
- _thumbOption (
85
- Constants .defaultGridThumbSize,
86
- Constants .defaultGridThumbSize,
87
- ),
105
+ _thumbOption (_thumbSize[0 ], _thumbSize[1 ]),
88
106
);
89
- } else if (imageFileType == ImageFileType .heic) {
90
- data = await (await key.entity.file)? .readAsBytes ();
91
- } else {
92
- data = await key.entity.originBytes;
93
107
}
94
- } else {
95
- final List <int > _thumbSize = thumbSize! ;
96
- data = await key.entity.thumbDataWithOption (
97
- _thumbOption (_thumbSize[0 ], _thumbSize[1 ]),
98
- );
99
- }
100
- if (data == null ) {
101
- throw AssertionError ('Null data in entity: $entity ' );
108
+ if (data == null ) {
109
+ throw StateError ('The data of the entity is null: $entity ' );
110
+ }
111
+ return decode (data);
112
+ } catch (e) {
113
+ // Depending on where the exception was thrown, the image cache may not
114
+ // have had a chance to track the key in the cache at all.
115
+ // Schedule a microtask to give the cache a chance to add the key.
116
+ Future <void >.microtask (() {
117
+ PaintingBinding .instance? .imageCache? .evict (key);
118
+ });
119
+ rethrow ;
102
120
}
103
- return decode (data);
104
121
}
105
122
106
123
ThumbOption _thumbOption (int width, int height) {
@@ -120,10 +137,11 @@ class AssetEntityImageProvider extends ImageProvider<AssetEntityImageProvider> {
120
137
/// ⚠ Not all the system version support read file name from the entity,
121
138
/// so this method might not work sometime.
122
139
/// 并非所有的系统版本都支持读取文件名,所以该方法有时无法返回正确的type。
123
- ImageFileType _getType () {
140
+ ImageFileType _getType ([ String ? filename] ) {
124
141
ImageFileType ? type;
125
- final String ? extension =
126
- entity.mimeType? .split ('/' ).last ?? entity.title? .split ('.' ).last;
142
+ final String ? extension = filename? .split ('.' ).last ??
143
+ entity.mimeType? .split ('/' ).last ??
144
+ entity.title? .split ('.' ).last;
127
145
if (extension != null ) {
128
146
switch (extension .toLowerCase ()) {
129
147
case 'jpg' :
0 commit comments