Reputation: 489
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
Reputation: 211
I had the same problem and my fix was to change documents
to docs
, see bellow:
snapshot.data.docs[index]
Upvotes: 21
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
Reputation: 21
I also had this problem.
My fix was to rewrite it as
snapshot.data[index]['name']
Upvotes: 2
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
Reputation: 31
I had the same issue. It was a typo.
snapshot.data.document[index]
should be:
snapshot.data.documents[index]
Upvotes: 3