I have used StreamBuilder, StreamController, BehaviorSubject, Stream etc.. for some time now. But I am still confused about some definitions. Especially for this question, Is StreamController and BehaviorSubject a Stream in Dart?
The reason why I have this question is because I can find the following words/quotes online:
From the above 4 points from online doc, it gives me: BehaviorSubject is an Observable and then is a Stream. So BehaviorSubject is a Stream. And BehaviorSubject is a StreamController too. Thus StreamController is a Stream.
But if StreamController is a Stream, that will contradict with some other articles that Stream is actually a part of StreamController and you get the Stream from StreamController.stream.
If we talk about Sink, there will be even more confusing.
StreamController implements StreamSink. So a Sink is a special StreamController.
So from all of the above words, I kinda get the following result:
BehaviorSubject = StreamController = Observable = Stream = Sink
In the end, everything are the same thing... Am I crazy?
Edited: (I understand now. Hope it's correct)
To clarify my confusion, I think I have to understand "BehaviorSubject is a special StreamController"
this sentence.
By googling and checking some sdk code, I think I understand that BehaviorSubject is a special StreamController, but not vice versa. That will solve my confusion. BehaviorSubject extends Subject, and Subject implements StreamController. Thus "BehaviorSubject is a special StreamController"
is correct. But I can't say StreamController is a BehaviorSubject. Thus I can't say StreamController is a Stream even if BehaviorSubject is actually a Stream.
I hope what I understand above is correct.
Streams provide an asynchronous sequence of data. Data sequences include user-generated events and data read from files. You can process a stream using either await for or listen() from the Stream API. Streams provide a way to respond to errors. There are two kinds of streams: single subscription or broadcast.
There are two types of streams in Flutter: single subscription streams and broadcast streams. Single subscription streams are the default.
BehaviorSubject<T> class Null safety. A special StreamController that captures the latest item that has been added to the controller, and emits that as the first item to any new listener. This subject allows sending data, error and done events to the listener.
StreamController<T> class Null safety. A controller with the stream it controls. This controller allows sending data, error and done events on its stream. This class can be used to create a simple stream that others can listen on, and to push events to that stream.
A StreamController
is a StreamController
.
It does not extend anything. So it is not any of Observable
, Stream
, BehaviorSubject
or Sink
.
It does implement Sink
as you said and thus allow you to add data on it directly, i.e. use streamController.add
as well as streamController.sink.add
. This data is then passed onto the Stream
that each controller carries.
BehaviorSubject
is not actually part of the standard library and just a fancy addition to streams from rxdart
. It allows you to access the latest value at any time directly.
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