Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is StreamController and BehaviorSubject a Stream in Dart?

Tags:

flutter

dart

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:

  • BehaviorSubject is a special StreamController... (from the API doc)
  • Observable is a a wrapper class that extends Stream. (Observable is a Stream)
  • BehaviorSubject extends Subject (BehaviorSubject is a Subject)
  • Subject extends Observable (Subject is a Observable)

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.

like image 871
sgon00 Avatar asked Apr 05 '19 16:04

sgon00


People also ask

What is a stream in Dart?

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.

What are different types of streams in flutter?

There are two types of streams in Flutter: single subscription streams and broadcast streams. Single subscription streams are the default.

What is BehaviorSubject in Dart?

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.

What is stream controller in flutter?

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.


Video Answer


1 Answers

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.

like image 54
creativecreatorormaybenot Avatar answered Nov 10 '22 16:11

creativecreatorormaybenot