如何使用StreamBuilder显示Firestore文档中的数据列表

时间:2019-05-08 12:51:40

标签: arrays flutter stream builder

我希望能够从Firestore数据库中文档内的数组创建列表。

我的数据库如下:

我希望能够从videosUrl数组中的URL创建streamBuilder网格。

我尝试了很多事情,但是我猜最好的方法就是这样:


StreamBuilder <List<DocumentSnapshot>>(
      stream: Firestore.instance
          .collection('events')
          .document('-LeH4rspnPTpeTLdt8hs')
          .collection('participants')
          .document('-LeL_TSfFDfqKgm-Io9T')
          .snapshots().asyncMap((snap) async {
        List<String> videosUrlArray = snap.data['videosUrl'];
        var videoUrlList = <DocumentSnapshot>[];
        for (var videoUrlPath in videosUrlArray) {
          videoUrlList.add(await Firestore.instance.document(videoUrlPath).get());
        }
        print(videoUrlList);
        return videoUrlList;

      }),
      builder: (BuildContext context, AsyncSnapshot<List<DocumentSnapshot>> snapshot) {
        if (snapshot.hasError)
          return new Text('Error: ${snapshot.error}');
        switch (snapshot.connectionState) {
          case ConnectionState.waiting: return _showLoading();
          default:
            return new GridView(
              reverse: false,
              gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2),
              children: snapshot.data.map((DocumentSnapshot document) {
                return Text('${snapshot.data}');
              }).toList(),
            );
        }
      },
    )

但是我仍然无法访问数据!

1 个答案:

答案 0 :(得分:0)

我使用以下代码解决了此问题:


Widget buildGridFilesToExport(){
  return new StreamBuilder(
    stream: Firestore.instance
        .collection('users')
        .document(dataUserGlobal.userAdminId)
        .collection('events')
        .document(dataUserGlobal.eventId)
        .snapshots(),
    builder: (context, snapshot) {
      print(snapshot);
      if (snapshot.hasError)
        return new Text('Error: ${snapshot.error}');
      switch (snapshot.connectionState) {
        case ConnectionState.waiting: return new Text('Loading...');
        default:
          List videosList = snapshot.data['thumbnailsUrl'];
          return
            videosList != null ?
            new GridView.count(
              crossAxisCount: 2,
              childAspectRatio: 1,
              children: List.generate(snapshot.data['thumbnailsUrl'].length, (index) {
                return Container(
                    padding: EdgeInsets.all(5.0),
                    child: Column(
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(bottom: 2.0),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(5.0)),
                            image: DecorationImage(
                              image: NetworkImage(snapshot.data['thumbnailsUrl'][index]),
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),

                      ],
                    )
                );
              }),
            )
                :
            Center(
                child: Container(
                  width: 300,
                  child: Text(
                    'Ancora nessun video!\nVai nella cartella amici e accetta i loro video!',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontFamily: 'acumin-pro',
                      fontSize: 22,
                    ),
                  ),
                )
            );
      }
    },
  );
}