listenToStream<T> method
Returns a StreamSubscription which handles events from the stream using
the provided onData
, onError
and onDone
handlers.
Consult documentation for Stream.listen for more info.
If the returned StreamSubscription
is cancelled manually (i.e. canceled
before disposal of the parent object) Disposable will clean up the
internal reference allowing the subscription to be garbage collected.
Neither parameter may be null
.
Implementation
@mustCallSuper
@override
StreamSubscription<T> listenToStream<T>(
Stream<T> stream, void onData(T event),
{Function? onError, void onDone()?, bool? cancelOnError}) {
_throwOnInvalidCall2('listenToStream', 'stream', 'onData', stream, onData);
var managedStreamSubscription = ManagedStreamSubscription(stream, onData,
onError: onError, onDone: onDone, cancelOnError: cancelOnError);
_logManageMessage(managedStreamSubscription);
var disposable = ManagedDisposer(() {
_logUnmanageMessage(managedStreamSubscription);
return managedStreamSubscription.cancel();
});
_internalDisposables.add(disposable);
managedStreamSubscription.didComplete.then((_) {
if (!_isDisposedOrDisposing) {
_logUnmanageMessage(disposable);
_internalDisposables.remove(disposable);
}
});
return managedStreamSubscription;
}