【问题标题】:How to build Flutter widget based on two Firestore collections如何基于两个 Firestore 集合构建 Flutter 小部件
【发布时间】:2019-08-11 00:31:27
【问题描述】:

我在 Firestore 中有两个集合:

活动:
标题
教练ID

教练:
名字

我用 StreamBuilder 制作了一个显示事件的小部件“EventsPage”。现在我需要这个小部件来显示每个事件的教练姓名——比如 coaches[event['coachId']['name']。如何在构建“EventsPage”小部件之前获取“教练”集合?

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    你可以使用嵌套的StreamBuilders

    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

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 2023-02-05
      相关资源
      最近更新 更多