Skip to content

Commit 1974436

Browse files
committed
Put initialization code in try-catch to mirror old behaviour of sending errors on stream
Signed-off-by: Justus Fluegel <justusfluegel@gmail.com>
1 parent 1e3196d commit 1974436

File tree

1 file changed

+54
-44
lines changed

1 file changed

+54
-44
lines changed

cached_network_image_web/lib/cached_network_image_web.dart

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -120,55 +120,65 @@ class ImageLoader implements platform.ImageLoader {
120120
) {
121121
var streamController = StreamController<ui.Codec>();
122122

123-
final stream = cacheManager.getFileStream(
124-
url,
125-
withProgress: true,
126-
headers: headers,
127-
key: cacheKey,
128-
);
123+
try {
124+
final stream = cacheManager.getFileStream(
125+
url,
126+
withProgress: true,
127+
headers: headers,
128+
key: cacheKey,
129+
);
129130

130-
var state = _State.open;
131+
var state = _State.open;
131132

132-
stream.listen(
133-
(event) {
134-
if (event is DownloadProgress) {
135-
chunkEvents.add(
136-
ImageChunkEvent(
137-
cumulativeBytesLoaded: event.downloaded,
138-
expectedTotalBytes: event.totalSize,
139-
),
140-
);
141-
}
142-
if (event is FileInfo) {
143-
if (state == _State.open) {
144-
state = _State.waitingForData;
133+
stream.listen(
134+
(event) {
135+
if (event is DownloadProgress) {
136+
chunkEvents.add(
137+
ImageChunkEvent(
138+
cumulativeBytesLoaded: event.downloaded,
139+
expectedTotalBytes: event.totalSize,
140+
),
141+
);
145142
}
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();
143+
if (event is FileInfo) {
144+
if (state == _State.open) {
145+
state = _State.waitingForData;
152146
}
147+
148+
event.file
149+
.readAsBytes()
150+
.then((value) => decode(value))
151+
.then((data) {
152+
streamController.add(data);
153+
if (state == _State.closing) {
154+
streamController.close();
155+
chunkEvents.close();
156+
}
157+
});
158+
}
159+
},
160+
onError: (e, st) {
161+
scheduleMicrotask(() {
162+
evictImage();
153163
});
154-
}
155-
},
156-
onError: (e, st) {
157-
scheduleMicrotask(() {
158-
evictImage();
159-
});
160-
streamController.addError(e, st);
161-
},
162-
onDone: () async {
163-
if (state == _State.open) {
164-
streamController.close();
165-
chunkEvents.close();
166-
} else if (state == _State.waitingForData) {
167-
state = _State.closing;
168-
}
169-
},
170-
cancelOnError: true,
171-
);
164+
streamController.addError(e, st);
165+
},
166+
onDone: () async {
167+
if (state == _State.open) {
168+
streamController.close();
169+
chunkEvents.close();
170+
} else if (state == _State.waitingForData) {
171+
state = _State.closing;
172+
}
173+
},
174+
cancelOnError: true,
175+
);
176+
} on Object catch (e, st) {
177+
scheduleMicrotask(() {
178+
evictImage();
179+
});
180+
streamController.addError(e, st);
181+
}
172182

173183
return streamController.stream;
174184
}

0 commit comments

Comments
 (0)