如何基于两个Firestore集合构建Flutter小部件 [英] How to build Flutter widget based on two Firestore collections
问题描述
我在Firestore中有两个收藏夹:
I have two collections in Firestore:
事件:
标题
coachId
events:
title
coachId
教练:
名称
coaches:
name
我用StreamBuilder创建了一个小部件"EventsPage",用于显示事件.现在,我需要该小部件来显示每个事件的教练名称-像coachs [event ['coachId'] ['name']之类的东西.在构建"EventsPage"小部件之前,如何获取"coachs"集合?
I made a widget 'EventsPage' with StreamBuilder which shows events. Now I need this widget to show coach name for every event - something like coaches[event['coachId']['name']. How can I get 'coaches' collection before 'EventsPage' widget is built?
推荐答案
您可以使用嵌套的StreamBuilder
s
You can use nested StreamBuilder
s
Widget nestedStreamBuilders() {
return StreamBuilder(
stream: firstStream,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) return Text('Error: ${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Select lot');
case ConnectionState.waiting:
return Text('Awaiting bids...');
case ConnectionState.active:
case ConnectionState.done:
return StreamBuilder(
stream: secondStream,
builder: (BuildContext context, AsyncSnapshot secondSnapshot) {
if (secondSnapshot.hasError)
return Text('Error: ${secondSnapshot.error}');
switch (secondSnapshot.connectionState) {
case ConnectionState.none:
return Text('Select lot');
case ConnectionState.waiting:
return Text('Awaiting bids...');
case ConnectionState.active:
case ConnectionState.done:
return BuildYourWidgetHere();
// you can build your widget here because we have the both data
return null; // unreachable
},
);
}
return null; // unreachable
},
);
}
如果您需要FutureBuilder
,请参见以下示例: FutureBuilder_Example
if you need FutureBuilder
this is the example : FutureBuilder_Example
这篇关于如何基于两个Firestore集合构建Flutter小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!