我正在使用:
StreamProvider
代码功能
FrediUserGroup数据。 var userGroups = Provider.of<List<FrediUserGroup>?>(context);
FrediUserGroup group = userGroups![widget.groupIndex]; group.participantsIds属性,如果从数据库中添加/删除某些id,则该属性将不断更新,作为所讨论流的参数: StreamBuilder(
stream: getGroupParticipantsDB(group.participantsIds),
builder: (BuildContext context, AsyncSnapshot<dynamic>
participantDataSnapshot) {
return MyWidgets() //any widget to display data
});使用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);}问题
当我删除数据库中的所有参与者时,会将again.
group.participantIds,并将其更新为空[]。流生成器的
getGroupParticipantsDB(group.participantsIds)则被称为group.participantIds CombineLatestStream仍然有旧的流(现在已删除的旧参与者,它们甚至不会出现在group.participantIds中),尽管这次streams列表是空的。H 238f 239问题
如何在再次调用CombineLatestStream之前删除/重置CombineLatestStream.list(streams)?因为它储存着我不再需要的旧的溪流。由于它是一个不可变的列表,我无法清除它。
发布于 2022-06-06 18:54:47
根据医生的说法
https://pub.dev/documentation/rxdart/latest/rx/CombineLatestStream-class.html
如果所提供的流为空,则得到的序列将立即完成,不会发出任何项,也不会调用组合器函数。
根据“颤振”的记录
https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html
生成的快照的数据和错误字段只有在状态为ConnectionState.active时才会更改。
我假设当您有一个空的id列表时,CombineLatestStream有一个空的流列表并立即关闭该流。由于流已关闭,StreamBuilder不提供新数据。
若要使用空的用户数组重新修改小部件,请尝试使用如下条件:
if (ids.isEmpty) {
return Stream.value([]);
}https://stackoverflow.com/questions/72447879
复制相似问题