Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...