Satrogan
Satrogan

Reputation: 489

Flutter Firestore QuerySnapshot has no instance of getter 'document

I'm using this to get a list of messages from the firestore database, however, it's giving me an error:

flutter: The following NoSuchMethodError was thrown building: flutter: Class 'QuerySnapshot' has no instance getter 'document'. flutter: Receiver: Instance of 'QuerySnapshot' flutter: Tried calling: document

The code that I'm using is :

StreamBuilder(
                stream: Firestore.instance
                    .collection('messages')
                    .document(groupId)
                    .collection(groupId)
                    .orderBy('timestamp', descending: true)
                    .snapshots(),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  if (!snapshot.hasData) {
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  } else {
                    listMessage = snapshot.data.documents;
                    return ListView.builder(
                      padding: EdgeInsets.all(10.0),
                      itemBuilder: (context, index) =>
                          buildItem(index, snapshot.data.document[index]),
                      itemCount: snapshot.data.documents.length,
                      reverse: true,
                      controller: scrollController,
                    );
                  }
                },
              ),

I'm new to Firestore and noSQL can anyone help here please?

Upvotes: 4

Views: 20979

Answers (5)

user14986466
user14986466

Reputation: 211

I had the same problem and my fix was to change documents to docs, see bellow:

snapshot.data.docs[index]

Upvotes: 21

Dangdat
Dangdat

Reputation: 431

buildSearchResult() {
    return FutureBuilder(
        future: searchResultFuture,
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return circularProgress();
          }
          // otherwise if it has data
          List<Text> searchResults = [];
          snapshot.data.**documents**.forEach((doc) {
            User user = User.fromDocument(doc);
            searchResults.add(Text(user.username));
          });
          return ListView(
            children: searchResults,
          );
        });
  }

I got similar problem, mine was because I omitted the letter 's' in documents as marked bold above. I wrote 'document' instead of 'documents'

Upvotes: 1

Dany G Sam
Dany G Sam

Reputation: 21

I also had this problem.
My fix was to rewrite it as

snapshot.data[index]['name']

Upvotes: 2

Satrogan
Satrogan

Reputation: 489

So the problem was in the builder:(BuildContext context, AsyncSnapshot snapshot) it should have been (BuildContext context, AsyncSnapshot'<'QuerySnapshot'>' snapshot) with that added you'll be able to access snapshot.data.documents

QuerySnapshot without the quotes around the angle brackets, I had to put them there for it to show up here on Stackoverflow.

Upvotes: 5

Stu McDonald
Stu McDonald

Reputation: 31

I had the same issue. It was a typo.

snapshot.data.document[index]

should be:

snapshot.data.documents[index]

Upvotes: 3

Related Questions