我在Firestore中有两个收藏集:
事件:
标题
coachId
教练:
名称
我使用StreamBuilder创建了一个小部件“ EventsPage”,用于显示事件。现在,我需要这个小部件来显示每个事件的教练名称-像coachs [event ['coachId'] ['name']之类的东西。在构建“ EventsPage”小部件之前,如何获取“教练”集合?
答案 0 :(得分:2)
您可以使用嵌套的StreamBuilder
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