Skip to content

Commit

Permalink
Don't prematurely close the stream
Browse files Browse the repository at this point in the history
Signed-off-by: Justus Fluegel <[email protected]>
  • Loading branch information
JustusFluegel committed Jun 5, 2024
1 parent 82f10f9 commit a72aa51
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions cached_network_image_web/lib/cached_network_image_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import 'package:cached_network_image_platform_interface'
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

enum _State { open, waitingForData, closing }

/// ImageLoader class to load images on the web platform.
class ImageLoader implements platform.ImageLoader {
@Deprecated('Use loadImageAsync instead')
Expand Down Expand Up @@ -125,6 +127,8 @@ class ImageLoader implements platform.ImageLoader {
key: cacheKey,
);

var state = _State.open;

stream.listen(
(event) {
if (event is DownloadProgress) {
Expand All @@ -136,9 +140,17 @@ class ImageLoader implements platform.ImageLoader {
);
}
if (event is FileInfo) {
event.file
.readAsBytes()
.then((value) => decode(value).then(streamController.add));
if (state == _State.open) {
state = _State.waitingForData;
}

event.file.readAsBytes().then((value) => decode(value)).then((data) {
if (state == _State.closing) {
streamController.close();
chunkEvents.close();
}
streamController.add(data);
});
}
},
onError: (e, st) {
Expand All @@ -148,8 +160,12 @@ class ImageLoader implements platform.ImageLoader {
streamController.addError(e, st);
},
onDone: () async {
streamController.close();
chunkEvents.close();
if (state == _State.open) {
streamController.close();
chunkEvents.close();
} else if (state == _State.waitingForData) {
state = _State.closing;
}
},
cancelOnError: true,
);
Expand Down

0 comments on commit a72aa51

Please sign in to comment.