@@ -12,6 +12,8 @@ import 'package:cached_network_image_platform_interface'
12
12
import 'package:flutter/material.dart' ;
13
13
import 'package:flutter_cache_manager/flutter_cache_manager.dart' ;
14
14
15
+ enum _State { open, waitingForData, closing }
16
+
15
17
/// ImageLoader class to load images on the web platform.
16
18
class ImageLoader implements platform.ImageLoader {
17
19
@Deprecated ('Use loadImageAsync instead' )
@@ -125,6 +127,8 @@ class ImageLoader implements platform.ImageLoader {
125
127
key: cacheKey,
126
128
);
127
129
130
+ var state = _State .open;
131
+
128
132
stream.listen (
129
133
(event) {
130
134
if (event is DownloadProgress ) {
@@ -136,9 +140,17 @@ class ImageLoader implements platform.ImageLoader {
136
140
);
137
141
}
138
142
if (event is FileInfo ) {
139
- event.file
140
- .readAsBytes ()
141
- .then ((value) => decode (value).then (streamController.add));
143
+ if (state == _State .open) {
144
+ state = _State .waitingForData;
145
+ }
146
+
147
+ event.file.readAsBytes ().then ((value) => decode (value)).then ((data) {
148
+ streamController.add (data);
149
+ if (state == _State .closing) {
150
+ streamController.close ();
151
+ chunkEvents.close ();
152
+ }
153
+ });
142
154
}
143
155
},
144
156
onError: (e, st) {
@@ -148,8 +160,12 @@ class ImageLoader implements platform.ImageLoader {
148
160
streamController.addError (e, st);
149
161
},
150
162
onDone: () async {
151
- streamController.close ();
152
- chunkEvents.close ();
163
+ if (state == _State .open) {
164
+ streamController.close ();
165
+ chunkEvents.close ();
166
+ } else if (state == _State .waitingForData) {
167
+ state = _State .closing;
168
+ }
153
169
},
154
170
cancelOnError: true ,
155
171
);
0 commit comments