In the function below, I want to get all the records in a collection named 'contexts'.
But, when I ask to print each record, I get nothing, not even an empty field or a null.
On the console, it is like if I have never ask for print.
I do not understand why.
Future getAllContextInFirebaseV1() async {
CollectionReference ref = FirebaseFirestore.instance.collection('Users')
.doc((FirebaseAuth.instance.currentUser.uid))
.collection('contexts');
QuerySnapshot contextsQuery = await ref
.get();
contextsQuery.docs.forEach((document) {
print(document['context_Name']);
});
}
CodePudding user response:
contextsQuery.docs
is a list of snapshots. You actually need to call .data()
on each document to access the data ('context_Name').
So instead of
print(document['context_Name']);
you should have
print((document.data() as Map<String, dynamic>)['context_Name']);
Note that the document has other properties on it like exists, id, reference, ...
But wait, are you sure that there are documents inside the contexts subcollection on the current user? Because chances are contextsQuery.size
could be zero, so forEach doesn't get called.
If yes, are you sure that the collection names are correct?