问题标签 [dart-stream]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
7146 浏览

flutter - 使用 Flutter 从 Firebase Firestore 查询中获取单个文档

我知道这可能看起来很明显,但我就是找不到办法做到这一点..

我正在尝试从 firebase 查询中获取单个文档。当我说单个文档时,我的意思不是流

到目前为止,我的方法是:

但是,我收到了错误A value of type 'StreamSubscription<QuerySnapshot>' can't be returned from method 'getDocument' because it has a return type of 'MyClass'.

谢谢!

0 投票
2 回答
2811 浏览

flutter - StreamBuilder 可以返回列表吗

AppBar颤动采取List<Widget>行动。出于某种原因,我找不到基于StreamBuilder.

我有一个 BehaviorSubject(可以更改为其他类型的流),我要在其中添加小部件,并且我想在 AppBar 中显示这些小部件(作为操作按钮)

那里的大多数示例都是针对“ ListView”使用的ListView.builder(...),这不适用于我的情况。

最终目标是提供使用 StreamBuilder 的actions属性AppBar并努力寻找方法。感谢您提供任何帮助或指示,并感谢您花时间阅读我的问题。

0 投票
1 回答
223 浏览

firebase - Flutter:何时激活 Firestore 文档/查询的侦听器

我正在计划一个使用 Cloud Firestore 作为后端的新 Flutter 应用程序。为了确保成本不会爆炸式增长,我想确定我的听众什么时候收听变化。

例如,当我使用以下代码通过集合进行查询时

并将其StreamProvider用作屏幕/脚手架的父级。当我在另一个屏幕上时,Flutter 是否也会监听“消息”集合中的变化?

0 投票
1 回答
720 浏览

flutter - 保持 Flutter dart 流打开以观察模型的变化

我是 Flutter/Dart 的新手,正在尝试创建一个基本的身份验证服务。我的简单做法是让我的主应用程序监听一个流,该流将在发生身份验证操作时发出用户。这就是很多 Firebase 示例的工作方式,唯一的区别是我想直接连接到我自己的 API。

为此,我创建了一个 AuthService,但实际上似乎无法使其正常工作。

我想要达到的目标

在顶层MyApp,我希望能够使用 AuthProvider 监听身份验证的变化。我应该能够在任何地方访问身份验证提供程序,例如

并使用它来管理应用程序。我在现有代码中遇到的问题是 Stream 状态会立即标记为已完成,因此不会从onAuthStateChanged. 实际上,我需要AuthWidget在应用程序的整个生命周期中继续监听,以便我可以响应登录/注销事件并相应地更改 UI。

任何帮助是极大的赞赏!

0 投票
1 回答
534 浏览

flutter - 流生成器(脏,状态:_StreamBuilderBaseState>#25d1b)

使用 Stream 构建器时出现错误。在我的代码中,我使用if(snapshot.data != null) { list.add(snapshot.data); }了但我得到了一个在 null 上调用 add 的 msg。我什至添加print('')了检查是否snapshot.data为空的语句,但它也可以与 print() 一起使用。所以我根本不明白如何解决这个错误。请有人回应。提前致谢。

这是我的代码

这是结果:

0 投票
1 回答
2008 浏览

dart - Can exceptions thrown in dart streams be handled by subscribers without closing the stream?

Short example of what I'm having trouble understanding:

With usage:

This will stop at 3 with the output:

From the documentation (https://dart.dev/tutorials/language/streams) and (https://api.dart.dev/stable/2.9.1/dart-async/Stream/handleError.html), this is as expected, as exceptions thrown will automatically close the stream.

  1. Does this mean that the correct way to handle exceptions in a stream, so that subscriptions can be long-lived in such an event, is to handle the exception inside the stream itself? That it is not possible to do so from the outside?
  2. Is this the same for broadcast streams?
  3. If I'm thinking about this in the wrong way, what are some pointers to start thinking right?

I'm currently thinking of streams as being a source of asynchronous data events that occasionally might be error events. From the documentation and examples, it all looks neat, but I'm thinking that wanting to handle errors and otherwise continue observing the data stream is a normal use case. I'm having a hard time writing the code to do so. But, I might be going about this wrong. Any insights will be much appreciated.


Edit: I can add that I've tried various things like using a stream transformer, with the same result:

Also, listen() has an optional argument cancelOnError that looks promising, but it defaults to false, so no cigar here.

0 投票
2 回答
51 浏览

flutter - 多路访问同一流

语境

下面的代码代表一种抽象,其中MyClass是某种下载管理器。

问题

我希望在执行此代码后,它会生成值 t1 和 t2,并且输出 t3 只会生​​成一次“正在下载”。例如:

但它会输出所有四个 t1值、八个 t3值,并且没有“正在下载”消息:

对我来说,这些t1值会正确输出,t2也会正确输出,并且t3会输出“正在下载”消息,因为一切都是异步运行的,它会尝试“下载”已经下载的内容(因为该test()方法是在MyClass) 的同一个实例上调用的。

我错过了什么?

0 投票
0 回答
715 浏览

flutter - Flutter:流和隔离

我正在尝试MyComplexObject在流中隔离。 MyComplexObject在它的类中有更多的对象。

MyComplexObject例子:

我的流功能

streamMyComplexObject与 a 一起使用StreamBuilder,是否可以隔离 的解析/转换MyComplexObject

0 投票
1 回答
295 浏览

flutter - 如何收听 Sembast 记录中地图内的单个值?

该文档谈到了聆听记录,但我如何聆听该记录中的单个字段?假设记录包含地图。

假设我有一个User类如下:

当我像这样在本地保存它时:

isVerified然后在页面的某个地方,我想在不获取全部User信息的情况下收听字段的更改。我怎么做?

因为_store.record(user.uid).onSnapshot(database)返回Stream<RecordSnapshot>User该类的全部数据。

谢谢

0 投票
2 回答
361 浏览

dart - 使用 dartz 中的 Either 时出现编译器错误

我目前正在试验颤振框架和飞镖,偶然发现了一个我无法理解的看似奇怪的行为。尽管实际问题发生的环境要复杂得多,但我什至能够在一个极其简化的展示中复制它:

虽然上面的示例编译不矛盾,但我确实收到以下示例的编译错误:

错误:“Left<String, dynamic>”类型的值不能分配给“Right<dynamic, int>”类型的变量

有没有人能够阐明这种方式?

################################################# ######

另一个例子:

}

这会编译但以运行时错误结束:类型“未来”不是类型“未来<Either<String, int>>”的子类型

然后,我尝试向编译器提供尽可能多的提示,以提出这种方法:

}

现在我得到: type 'Left<String, dynamic>' is not a subtype of type 'Either<String, int>' in type cast

我真的无法理解这个