When i using BLOC in Flutter , for Example:
class StreamText extends StatelessWidget {
StreamText(
this.stream, {
this.style,
});
final Stream<dynamic> stream;
final TextStyle style;
@override
Widget build(BuildContext context) {
return StreamBuilder<dynamic>(
stream: stream,
builder: (context, snapshot) {
return Text(snapshot.data.toString(), style: style);
},
);
}
}
This is a Stateless widget and don't have dispose()
method there;
and how could i off the stream or will it auto off the stream when this widget destroyed?
Stateless Widget: The widgets whose state can not be altered once they are built are called stateless widgets. These widgets are immutable once they are built i.e any amount of change in the variables, icons, buttons, or retrieving data can not change the state of the app.
If you need to build a widget based on the result of a Stream , you can use the StreamBuilder widget. You can create a Stream and pass it as the stream argument. Then, you have to pass an AsyncWidgetBuilder function which is used to build a widget based on the snapshots of the Stream .
A widget is either stateful or stateless. If a widget can change—when a user interacts with it, for example—it's stateful. A stateless widget never changes. Icon , IconButton , and Text are examples of stateless widgets.
initializing a controller should be a one-time operation; if you do it on a StatelessWidget's build method, it will be triggered every time this widget is rebuilt. If you do it on a StatefulWidget's initState, it will only be called once, when this object is inserted into the tree when the State is initialized.
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.
In Stateless widget, the StreamBuilder itself will "auto-off" when the widget will be removed from the Widget tree. You don't have to handle anything.
BUT, if you have a StreamController which sends the snapshots you should close it manually when you're done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With