junk
junk

Reputation: 987

Will StreamBuilder auto off the stream in stateless widget?

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?

Upvotes: 14

Views: 9952

Answers (4)

Evin1_
Evin1_

Reputation: 12866

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.

Upvotes: 10

Kirill Karmazin
Kirill Karmazin

Reputation: 6736

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.

Upvotes: 5

Francesco Mineo
Francesco Mineo

Reputation: 137

You are passing a stream into a Stateless widget, you should close the stream overriding the dispose method of the subclassed State class associated to the Stateful widget where you have the instance of the stream. Also, in this widget, you have to check if the snapshot.data is null (and return for example a Container()) or you get an error since you are not passing an initialData to the StreamBuilder.

Upvotes: 1

Edman
Edman

Reputation: 5605

No, it won't auto close. In general the owner of the stream is the one who manages the stream state.

A good solution in my opinion is to make a stateful widget own your BLoCs and close the streams in its dispose method.

This article shows a possible way to implement this, have a look at the BlocProvider class.

Upvotes: 5

Related Questions