从CombineLatestStream -RXDART中删除流
我正在使用:
- 提供商软件包 - stream -provider
- rxdart软件包 - combineLatestStream
- firebase实时数据库 - onValue流
码功能
- 我从在应用程序开头声明的提供商中获取所有我的
frediusergroup
数据。
var userGroups = Provider.of<List<FrediUserGroup>?>(context);
FrediUserGroup group = userGroups![widget.groupIndex];
- 我使用
group.participantIds
属性,如果从数据库中添加/删除了某些ID,则可以不断更新,作为所讨论流的参数:
StreamBuilder(
stream: getGroupParticipantsDB(group.participantsIds),
builder: (BuildContext context, AsyncSnapshot<dynamic>
participantDataSnapshot) {
return MyWidgets() //any widget to display data
});
- 使用
rxdart
, 来自这些参与者的数据被加载到combineLatestStream
Stream<List<FrediUser>>? getGroupParticipantsDB(List<String>? participantIds) {
List<Stream<FrediUser>> streams = [];
participantIds?.forEach((id) {
var streamToAdd = dbQuery(2, 'users', id).onValue.map((event) =>
FrediUser.fromMap(event.snapshot.value as Map));
streams.add(streamToAdd);
});
return CombineLatestStream.list(streams);}
问题
- 当我删除数据库中的所有参与者时,
- 中,
group.participantids
已更改并更新为空[]。 streamBuilder再次被调用,流式getGrouPparticipantsDB(group.participantIds)
再次调用。 - 但是,
combineLatestStream
仍然具有旧流(现在已删除的旧参与者,甚至在group.participantids
>中都没有出现,尽管streams
列表这次是空的。
问题
如何在调用 commineLatestStream
再次调用 compineLatestStream.list(stream)
之前删除/重置 combineLatestStream
?因为它正在存储我不再需要的旧流。由于它是一个不变的列表,所以我无法清除()。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据文档,rxdart
并且根据flutter
https://api.flutter.dev/flutter.dev/flutter.dev/flutter.dev/flutter.dev/flutter.dev/flutter.dev/flutter.dev/-flutter.flutt 例, /widgets/streambuilder-class.html
我假设,当您拥有一个空的ID列表时,CombineLatestStream具有一个空的流列表,并立即关闭流。由于流关闭,因此StreamBuilder没有提供商新数据。
为了用空数字来启动小部件,尝试使用这样的条件:
According to the docs rxdart
https://pub.dev/documentation/rxdart/latest/rx/CombineLatestStream-class.html
and according to the docs of flutter
https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html
I assume that when you have an empty list of id, CombineLatestStream has an empty list of streams and closes the stream immediately. Since the stream is closed, StreamBuilder doesn't provider new data.
To rerender your widget with empty array of users try to use a condition like this: