繁体   English   中英

(Flutter - Firestore) 在 null 上调用了 getter 'documents'

[英](Flutter - Firestore) The getter 'documents' was called on null

当图像在 firebase 存储中时,我已将图像的下载链接存储在 Firestore 上。

我正在尝试通过 stream 构建器检索链接并显示它们,但我遇到了错误。

我能做些什么来解决这个问题。

流生成器:

StreamBuilder(
                stream: db
                    .collection("Highway Secondary School Announcements")
                    .doc()
                    .snapshots(),
                builder: (context, snapshot) {
                  if (snapshot!= null && snapshot.hasData) {
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  } else if (snapshot.hasError) {
                    return Center(
                      child: Text("Snapshot Was Not Retrieved"),
                    );
                  }

                  for (int i = 0; i < snapshot.data.documents; i++) {
                    listOfUrls.add(snapshot.data.documents[i]['url']);

                    listOfPics.add(Padding(
                      padding: const EdgeInsets.only(top: 50, bottom: 50),
                      child: Image.network(listOfUrls[i]),
                    ));
                  }
                  return Container(
                    child: ListView(
                      children: listOfPics,
                    ),
                  );
                }),

错误:

The getter 'documents' was called on null.
Receiver: null
Tried calling: documents

如果您有 doc(),则需要指定要阅读的文档(doc('name')),否则,您可以删除“.doc()”。

参考: Cloud Firestore

db.collection("Highway Secondary School Announcements").doc().snapshots()

db.collection("Highway Secondary School Announcements").snapshots()

第二个问题:我使用“final items = snapshot.data?.docs”从该快照中获取文档。 这里有一个很好的例子Cloud Firestore flutter

        final items = snapshot.data?.docs.reversed;
        for ( var item in items!) {
          final itemName = item.get('name');
          final itemLogo = item.get('logo');
          final itemDate = item.get('date');

          // String itemDate2 = DateTime.fromMillisecondsSinceEpoch(itemDate).toString();
          final itemBubble = _getListItemWidget(
              iconName: itemLogo,
              titleName: itemName,
              subTitleName: DateTime.now(),
              scoreKeeper1: scoreKeeper1,
              scoreKeeper2: scoreKeeper2,
              scoreKeeper3: scoreKeeper3
          );
          itemBubbles.add(itemBubble);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM