No, it won't close the Stream
, but it will close the StreamSubscription
that is used to build the Widget.
If the Stream
is not going to be used for anything else, the best would be to dispose the Stream
somehow (by wrapping it on a StatefulWidget
or by using a BlocProvider
approach).
If you are using a Stream
somewhere else or you will use the Stream
in the future you don't need to worry about leaking memory for its use on a StreamBuilder
. As long as you dispose it whenever everyone else stops using it.
The StreamBuilder
itself extends from StreamBuilderBase
which is a StatefulWidget
, and it handles the StreamSubscription
with its own dispose
method.
This is an excerpt from the async.dart
library.
/// State for [StreamBuilderBase].
class _StreamBuilderBaseState<T, S> extends State<StreamBuilderBase<T, S>> {
StreamSubscription<T> _subscription;
@override
void initState() {
//...
_subscribe();
}
@override
void dispose() {
_unsubscribe();
super.dispose();
}
void _subscribe() {
if (widget.stream != null) {
_subscription = widget.stream.listen((T data) {
//...
}
}
void _unsubscribe() {
if (_subscription != null) {
_subscription.cancel();
_subscription = null;
}
}
}
As you can see, the StreamSubscription
is initialized on the initState
and automatically canceled on the dispose
call of the State, so the subscription used here will be always closed and you don't need to worry about it.
"off the stream"
? what do you mean by that?StreamController.close()
? – Overburden