Skip to content

Commit

Permalink
Close chunk events stream controller when imageStream is disposed
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmartos96 committed Oct 22, 2022
1 parent 3fcd65a commit ad4dea3
Showing 1 changed file with 25 additions and 2 deletions.
Expand Up @@ -77,7 +77,7 @@ class CachedNetworkImageProvider
ImageStreamCompleter load(
image_provider.CachedNetworkImageProvider key, DecoderCallback decode) {
final chunkEvents = StreamController<ImageChunkEvent>();
return MultiImageStreamCompleter(
final imageStreamCompleter = MultiImageStreamCompleter(
codec: _loadAsync(key, chunkEvents, decode),
chunkEvents: chunkEvents.stream,
scale: key.scale,
Expand All @@ -89,6 +89,10 @@ class CachedNetworkImageProvider
);
},
);

_handleChunkEventsClose(imageStreamCompleter, chunkEvents);

return imageStreamCompleter;
}

@Deprecated(
Expand Down Expand Up @@ -118,7 +122,7 @@ class CachedNetworkImageProvider
ImageStreamCompleter loadBuffer(image_provider.CachedNetworkImageProvider key,
DecoderBufferCallback decode) {
final chunkEvents = StreamController<ImageChunkEvent>();
return MultiImageStreamCompleter(
final imageStreamCompleter = MultiImageStreamCompleter(
codec: _loadBufferAsync(key, chunkEvents, decode),
chunkEvents: chunkEvents.stream,
scale: key.scale,
Expand All @@ -130,6 +134,10 @@ class CachedNetworkImageProvider
);
},
);

_handleChunkEventsClose(imageStreamCompleter, chunkEvents);

return imageStreamCompleter;
}

Stream<ui.Codec> _loadBufferAsync(
Expand Down Expand Up @@ -164,6 +172,21 @@ class CachedNetworkImageProvider
return false;
}

void _handleChunkEventsClose(
ImageStreamCompleter imageStreamCompleter,
StreamController<ImageChunkEvent> chunkEvents,
) {
// Make sure to close the chunk events controller after
// the image stream disposes. Otherwise reporting an image chunk
// event could fail beacause the ImageStream has been disposed.
// (https://github.com/Baseflow/flutter_cached_network_image/issues/785)
imageStreamCompleter.addOnLastListenerRemovedCallback(() {
if (!chunkEvents.isClosed) {
chunkEvents.close();
}
});
}

@override
int get hashCode => Object.hash(cacheKey ?? url, scale, maxHeight, maxWidth);

Expand Down

0 comments on commit ad4dea3

Please sign in to comment.