简体   繁体   English

StreamController 和 BehaviorSubject 是 Dart 中的流吗?

[英]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:我之所以有这个问题是因为我可以在网上找到以下单词/语录:

  • BehaviorSubject is a special StreamController... (from the API doc ) BehaviorSubject 是一个特殊的 StreamController...(来自API 文档
  • Observable is aa wrapper class that extends Stream. Observable 是一个扩展 Stream 的包装类。 (Observable is a Stream) (Observable 是一个流)
  • BehaviorSubject extends Subject (BehaviorSubject is a Subject) BehaviorSubject 扩展主题(BehaviorSubject 是一个主题)
  • Subject extends Observable (Subject is a Observable) Subject 扩展了 Observable (Subject 是一个 Observable)

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 . StreamControllerStreamController
It does not extend anything.它不扩展任何东西。 So it is not any of Observable , Stream , BehaviorSubject or Sink .所以它不是ObservableStreamBehaviorSubjectSink中的任何一个。

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.addstreamController.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替换

rxdart-changelog rxdart 更改日志

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较stream,streamController dart中的每个事件 - How to compare each events in stream, streamController dart 在 Dart 中,为什么要等待一个微不足道的(“空”)流控制器 stream 立即退出程序? - In Dart, why waiting for a trivial (“empty”) StreamController stream immediately exits program? 具有两个参数的Dart StreamController - Dart StreamController with two arguments Dart是否有任何方法可以从stream.listen()调用onDone回调,其中流来自StreamController - Dart is there any way to invoke onDone callback from stream.listen() where the stream is from StreamController btw `BehaviorSubject` 和 `Sink` 有什么关系吗? 和`StreamController`? - Are there any relationships btw `BehaviorSubject` and `Sink`? and `StreamController`? 更改StreamController后按钮保持禁用状态 <String> .broadcast()到BehaviorSubject <String> () - Button stays disabled after changing StreamController<String>.broadcast() to BehaviorSubject<String>() Java 中 Dart 的 StreamController 的等价物是什么? - What is the equivalent of Dart's StreamController in Java? Dart / rxdart / Bloc:在收听BehaviorSubject的流时接收具有原始类型的事件项 - Dart / rxdart / Bloc: receive event item with original type when listening to a BehaviorSubject's stream 从多个小部件访问 StreamController Stream - Access StreamController Stream from multiple widgets 流侦听器和流控制器异步“交谈”未按预期工作 - Stream listener and streamController "talking" asynchronously not working as expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM