Add some sanity to the ImageStream listener API (#32936)

The current API was broken in that you registered multiple
callbacks at once, but when you removed listeners, only the
primary listener was used to determine what was removed.
This led to unintuitive cases where the caller could get
unexpected behavior.

This updates the API to add and remove listeners using
a newly introduced [ImageStreamListener] object, a value
object that has references to the individual callbacks
that may fire.

flutter/flutter#24722
flutter/flutter#32374
flutter/flutter#32935
This commit is contained in:
Todd Volkert
2019-05-20 17:40:03 -07:00
committed by GitHub
parent d31ce31a27
commit 592f81e7c4
12 changed files with 242 additions and 267 deletions

View File

@@ -44,9 +44,17 @@ class _ImageLoaderState extends State<ImageLoader> {
// http client.
final NetworkImage image = NetworkImage('https://github.com/flutter/flutter');
final ImageStream stream = image.resolve(ImageConfiguration.empty);
stream.addListener((ImageInfo info, bool syncCall) {}, onError: (dynamic error, StackTrace stackTrace) {
print('ERROR caught by framework');
});
ImageStreamListener listener;
listener = ImageStreamListener(
(ImageInfo info, bool syncCall) {
stream.removeListener(listener);
},
onError: (dynamic error, StackTrace stackTrace) {
print('ERROR caught by framework');
stream.removeListener(listener);
},
);
stream.addListener(listener);
super.initState();
}