我有2个流,我需要合并建立一个小部件,但不像其他问题,我看到我需要嵌套我的流。
我有一个从Firestore获取文档集合的流,以及一个依赖于第一个获取文档子集合的数据的流。我想将这些合并合并到一个流中,但它们需要嵌套,因为每个文档都有自己的文档子集合。
流1(从FireStore获取习惯集合):
Stream<List> getHabits(){
final Stream<QuerySnapshot> documents = Firestore.instance
.collection("users")
.document('VtL1sxOoCOdJaOTT87IbMRwBe282')
.collection("habits")
.snapshots();
Stream<List> data = documents.map((doc) {
List data;
final documents = doc.documents;
///Maybe this would work to get history of each doc?
for(int i = 0; i < documents.length; i++){
///not sure what to do
getHistory(documents[i].documentID, DateTime.utc(2019,7,7), DateTime.now());
}
data = documents.map((documentSnapshot) => documentSnapshot).toList();
return data;
});
return data;
}
字符串
流2(在流1中调用,以DocumentID
为参数,获取文档的子集合):
Stream<List> getHistory(String id, DateTime start, DateTime end) async* {
await for (QuerySnapshot querySnapshot in Firestore.instance
.collection("users")
.document('VtL1sxOoCOdJaOTT87IbMRwBe282')
.collection("habits")
.document(id)
.collection("history")
.where('day', isGreaterThanOrEqualTo: start)
.where('day', isLessThanOrEqualTo: end)
.snapshots()) {
List history;
final documents = querySnapshot.documents;
history = documents.map((documentSnapshot) => documentSnapshot).toList();
yield history;
}
}
型
如何将这些流以嵌套格式组合成一个流,以便在Flutter中与StreamBuilder
一起使用?
编辑
我不知道我是否在正确的方向上工作或没有,但我已经试图实施解决方案,从斯彭斯特,这是我目前除了上述职能。
StreamBuilder<List>(
stream: getHabits(),
initialData: [],
builder: (context, snapshot) {
List<UserHabit> habits = [];
List<Widget> test = List.generate(snapshot.data.length, (index){
List<History> history = [];
DocumentSnapshot doc = snapshot.data[index];
return StreamBuilder(
stream: getHistory(doc.documentID, DateTime.utc(2019,7,7), DateTime.now()),
builder: (context, snapshot) {
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.waiting: return new Text('Loading...');
default:
if(!snapshot.data.isEmpty){ //history collection exists
for(int i = 0; i < snapshot.data.length; i++){
//add to history
history.add(History(
day: snapshot.data[i]['day'].toDate(),
dateCompleted: snapshot.data[i]['dateCompleted'].toDate(),
morning: snapshot.data[i]['morning'],
afternoon: snapshot.data[i]['afternoon'],
evening: snapshot.data[i]['evening'],
anytime: snapshot.data[i]['anytime'],
));
}
}
habits.add(UserHabit(
name: doc['habit'],
color: doc['color'],
icon: doc['icon'],
repeat: doc['repeat'],
daily: doc['daily'],
weekly: doc['weekly'],
monthly: doc['monthly'],
time: doc['time'],
history: history,
));
print(habits); //returns each iteration of assembling the list
return Text("i dont want to return anything");
}
},
);
}
);
print(habits); //returns empty list before anything is added
return Column(
children: test,
);
},
),
型
UserHabits和History的类可以共享,但它们只是分配类型并允许轻松访问的基本类。
2条答案
按热度按时间bkhjykvo1#
我已经简单地使用嵌套的
StreamBuilders
做了类似的事情。根据您希望如何组织Widget
,您可以在外部StreamBuilder
中创建流。根据您的澄清评论,这是一种可能性:字符串
sf6xfgos2#
尝试使用类似
Observable.zip2(stream1,stream2,zipper)
或Observable.combineLatest2(streamA, streamB, combiner)
的方法合并流。有关更多信息,请查看此post