Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between streamController.add() and streamController.sink.add()?

Tags:

flutter

dart

There are two ways that i know to add data to streamcontroller, one directly and other by use of a sink. I tried to read docs of Sink but i am not able to understand its concept like piping of data etc.

like image 665
Ayush P Gupta Avatar asked Jul 18 '18 07:07

Ayush P Gupta


People also ask

How to insert something into a stream using a streamcontroller?

to insert something into the Stream, the StreamController exposes the “ _entrance_ ”, called a StreamSink, accessible via the sink property the way out of the Stream, is exposed by the StreamController via the stream property (*) I intentionally used the term “ _usually_ ”, since it is very possible not to use any StreamController.

What is a streamcontroller in Salesforce?

StreamController: A StreamController simplifies stream management, automatically creating a stream and sink, and providing methods for controlling a stream's behavior. StreamSubscription: Listeners on a stream can save a reference to their subscription, which will allow them to pause, resume, or cancel the flow of data they receive.

How do I react to events in a streamcontroller?

With a StreamController instance, you can access a stream to listen for and react to data events using the Stream instance's listen () method, and you can access a sink to add new data events to the stream using the add () method of EventSink.

What is the difference between sink and streams?

Sink and Streams are both part of the stream controller. You add a data to the stream controller using sink which can be listened via the stream


1 Answers

Nothing. This does the same thing internally.

The real purpose of .sink property is to pass it as parameter of other object. Such as :

MyClass(   sink: myController.sink, ) 

This prevents classes to access to properties they shouldn't be able to.

But StreamController implements Sink so what's the point ?

Well true. But casting StreamController to Sink is different than creating a Sink.

For example, the class that uses Sink could very well do the following :

StreamSink sink = StreamController(); if (sink is StreamController) { // this is true     // access StreamController custom methods } 

The sink field is here to prevent this. It translates into the following :

StreamSink sink = StreamController().sink; if (sink is StreamController) { // false this time    // never reached } 
like image 186
Rémi Rousselet Avatar answered Sep 20 '22 07:09

Rémi Rousselet