streamController.add()和streamController.sink.add()有什么区别? [英] What is the difference between streamController.add() and streamController.sink.add()?
问题描述
我知道有两种方法可以将数据添加到streamcontroller中,一种是直接添加,另一种是通过使用接收器添加。我尝试阅读Sink的文档,但无法理解其概念,例如数据管道等。
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.
推荐答案
没什么。这在内部做同样的事情。
Nothing. This does the same thing internally.
.sink
属性的真正目的是将其作为其他对象的参数传递。例如:
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.
但是 StreamController
实现了 Sink
,这有什么意义?
But StreamController
implements Sink
so what's the point ?
是的。但是将 StreamController
转换为 Sink
不同于创建 Sink
。
Well true. But casting StreamController
to Sink
is different than creating a Sink
.
例如,使用 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
}
这篇关于streamController.add()和streamController.sink.add()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!