简体   繁体   中英

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

I have stored the download links of images on Firestore while the images are in firebase storage.

I am trying to retrieve the links and display them via stream builder but I'm encountering an error.

What can I do to fix this.

StreamBuilder:

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,
                    ),
                  );
                }),

Error:

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

If you have doc(), you need to specify which documentation(doc('name')) you want to be read, otherwise, you could remove ".doc()".

Reference: Cloud Firestore

before

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

after

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

Second question: I used "final items = snapshot.data?.docs" to get documents from that snapshot. Here has a nice example 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);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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