从CombineLatestStream -RXDART中删除流

发布于 2025-02-03 05:19:10 字数 1971 浏览 5 评论 0 原文

我正在使用:

  • 提供商软件包 - stream -provider
  • rxdart软件包 - combineLatestStream
  • firebase实时数据库 - onValue流

码功能

  1. 我从在应用程序开头声明的提供商中获取所有我的 frediusergroup 数据。
 var userGroups = Provider.of<List<FrediUserGroup>?>(context);
 FrediUserGroup group = userGroups![widget.groupIndex]; 
  1. 我使用 group.participantIds 属性,如果从数据库中添加/删除了某些ID,则可以不断更新,作为所讨论流的参数:
    StreamBuilder(
        stream: getGroupParticipantsDB(group.participantsIds),
        builder: (BuildContext context, AsyncSnapshot<dynamic> 
            participantDataSnapshot) {
            return MyWidgets() //any widget to display data
        });
  1. 使用 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 ?因为它正在存储我不再需要的旧流。由于它是一个不变的列表,所以我无法清除()。

I am using:

  • Provider Package - StreamProvider
  • RxDart package - CombineLatestStream
  • Firebase Realtime Database - onValue Stream

Code Functionality

  1. I get all my FrediUserGroup data from a provider declared at the beginning of the app.
 var userGroups = Provider.of<List<FrediUserGroup>?>(context);
 FrediUserGroup group = userGroups![widget.groupIndex]; 
  1. I use the group.participantsIds property, that is constantly updated if some id is added/deleted from the database, as an argument for the stream in question:
    StreamBuilder(
        stream: getGroupParticipantsDB(group.participantsIds),
        builder: (BuildContext context, AsyncSnapshot<dynamic> 
            participantDataSnapshot) {
            return MyWidgets() //any widget to display data
        });
  1. Using rxDart, the data from those participants is loaded into a 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);}

Problem

  • When I delete all participants in the database, group.participantIds is changed and updated to empty [].
  • The streambuilder gets called again, and the stream getGroupParticipantsDB(group.participantsIds) gets called again.
  • However, the CombineLatestStream still has the old streams (old participants that are now deleted, which don't even appear in the group.participantIds), eventhough the streams list is empty this time around.

Question

How can I delete/reset the CombineLatestStream before calling CombineLatestStream.list(streams) again? Because it is storing the old streams which I no longer need. As it is an immutable list, I cannot clear() it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世蒼涼 2025-02-10 05:19:10

根据文档,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没有提供商新数据。

为了用空数字来启动小部件,尝试使用这样的条件:

if (ids.isEmpty) {
  return Stream.value([]);
}

According to the docs rxdart

https://pub.dev/documentation/rxdart/latest/rx/CombineLatestStream-class.html

If the provided streams is empty, the resulting sequence completes
immediately without emitting any items and without any calls to the
combiner function.

and according to the docs of flutter

https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html

The data and error fields of snapshots produced are only changed when
the state is ConnectionState.active.

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:

if (ids.isEmpty) {
  return Stream.value([]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文