addListener method
- ImageStreamListener listener
Adds a listener callback that is called whenever a new concrete ImageInfo object is available or an error is reported. If a concrete image is already available, or if an error has been already reported, this object will notify the listener synchronously.
If the ImageStreamCompleter completes multiple images over its lifetime, this listener's ImageStreamListener.onImage will fire multiple times.
The listener will be passed a flag indicating whether a synchronous call occurred. If the listener is added within a render object paint function, then use this flag to avoid calling RenderObject.markNeedsPaint during a paint.
If a duplicate listener
is registered N times, then it will be called N
times when the image stream completes (whether because a new image is
available or because an error occurs). Likewise, to remove all instances
of the listener, removeListener would need to called N times as well.
When a listener
receives an ImageInfo object, the listener
is
responsible for disposing of the ImageInfo.image.
See also:
- addEphemeralErrorListener, which adds an error listener that is automatically removed after first image load or error.
Implementation
void addListener(ImageStreamListener listener) {
_checkDisposed();
_hadAtLeastOneListener = true;
_listeners.add(listener);
if (_currentImage != null) {
try {
listener.onImage(_currentImage!.clone(), !_addingInitialListeners);
} catch (exception, stack) {
reportError(
context: ErrorDescription('by a synchronously-called image listener'),
exception: exception,
stack: stack,
);
}
}
if (_currentError != null && listener.onError != null) {
try {
listener.onError!(_currentError!.exception, _currentError!.stack);
} catch (newException, newStack) {
if (newException != _currentError!.exception) {
FlutterError.reportError(
FlutterErrorDetails(
exception: newException,
library: 'image resource service',
context: ErrorDescription('by a synchronously-called image error listener'),
stack: newStack,
),
);
}
}
}
}