[英]Is StreamController and BehaviorSubject a Stream in Dart?
I have used StreamBuilder, StreamController, BehaviorSubject, Stream etc.. for some time now.我使用 StreamBuilder、StreamController、BehaviorSubject、Stream 等已经有一段时间了。 But I am still confused about some definitions.
但我仍然对一些定义感到困惑。 Especially for this question, Is StreamController and BehaviorSubject a Stream in Dart?
尤其是这个问题,StreamController 和 BehaviorSubject 是 Dart 中的 Stream 吗?
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.从在线文档的上述 4 点来看,它给了我: BehaviorSubject 是一个 Observable,然后是一个 Stream。 So BehaviorSubject is a Stream.
所以 BehaviorSubject 是一个流。 And BehaviorSubject is a StreamController too.
并且 BehaviorSubject 也是一个 StreamController。 Thus StreamController is a Stream.
因此 StreamController 是一个 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.但是如果 StreamController 是一个 Stream,这将与其他一些文章相矛盾,即 Stream 实际上是 StreamController 的一部分,并且您从 StreamController.stream 中获取 Stream。
If we talk about Sink, there will be even more confusing.如果我们谈论 Sink,将会更加混乱。
StreamController implements StreamSink. StreamController 实现了 StreamSink。 So a Sink is a special StreamController.
所以 Sink 是一个特殊的 StreamController。
So from all of the above words, I kinda get the following result:因此,从以上所有单词中,我得到以下结果:
BehaviorSubject = StreamController = Observable = Stream = Sink 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.为了澄清我的困惑,我想我必须理解
"BehaviorSubject is a special StreamController"
这句话。
By googling and checking some sdk code, I think I understand that BehaviorSubject is a special StreamController, but not vice versa .通过谷歌搜索和检查一些 sdk 代码,我想我明白 BehaviorSubject 是一个特殊的 StreamController,但反之则不然。 That will solve my confusion.
这将解决我的困惑。 BehaviorSubject extends Subject, and Subject implements StreamController.
BehaviorSubject 扩展了 Subject,Subject 实现了 StreamController。 Thus
"BehaviorSubject is a special StreamController"
is correct.因此
"BehaviorSubject is a special StreamController"
是正确的。 But I can't say StreamController is a BehaviorSubject.但我不能说 StreamController 是一个 BehaviorSubject。 Thus I can't say StreamController is a Stream even if BehaviorSubject is actually a Stream.
因此,即使 BehaviorSubject 实际上是 Stream,我也不能说 StreamController 是 Stream。
I hope what I understand above is correct.我希望我上面的理解是正确的。
A StreamController
is a StreamController
. StreamController
是StreamController
。
It does not extend anything.它不扩展任何东西。 So it is not any of
Observable
, Stream
, BehaviorSubject
or Sink
.所以它不是
Observable
、 Stream
、 BehaviorSubject
或Sink
中的任何一个。
It does implement Sink
as you said and thus allow you to add data on it directly, ie use streamController.add
as well as streamController.sink.add
.如您所说,它确实实现了
Sink
,因此允许您直接在其上添加数据,即使用streamController.add
和streamController.sink.add
。 This data is then passed onto the Stream
that each controller carries.然后将此数据传递到每个控制器携带的
Stream
上。
BehaviorSubject
is not actually part of the standard library and just a fancy addition to streams from rxdart
. BehaviorSubject
实际上并不是标准库的一部分,只是对来自rxdart
的流的一个花哨的补充。 It allows you to access the latest value at any time directly.它允许您随时直接访问最新值。
Not crazy at all, A stream is just like a pipe that take in data as a sink and give it back as a stream, while StreamController is just use to control the the stream.一点也不疯狂,流就像一个管道,将数据作为接收器接收并作为流返回,而 StreamController 只是用于控制流。 i hope this is helpful
我希望这是有帮助的
Observable
class is deprecated in rxdart 0.23.1
Observable
类在rxdart 0.23.1
中已弃用
Observable
can be replaced by Stream
Observable
可以被Stream
替换
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.