I need to get the data of the first document (only) of my sub collection.
I have tried this first solution :
void _getOwner(accommodation) async {
await FirebaseFirestore.instance
.collection('Users')
.doc(uid)
.collection('Accommodation')
.doc(accommodation.id)
.collection('Owner')
.limit(1)
.get()
.then((value){
print(value.data());
});
}
But I have this error message :
The method 'data' isn't defined for the class 'QuerySnapshot'.
- 'QuerySnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('../../../.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.15.0/lib/cloud_firestore.dart'). Try correcting the name to the name of an existing method, or defining a method named 'data'. print(value.data());
Then I have tried this solution :
void _getOwner(accommodation) async {
await FirebaseFirestore.instance
.collection('Users')
.doc(uid)
.collection('Accommodation')
.doc(accommodation.id)
.collection('Owner')
.get()
.then((QuerySnapshot querySnapshot) => {
querySnapshot.docs.forEach((doc) {
print(doc["LastName"]);
})
});
}
It works, but I need the LastName of my first Owner document, not "forEach"... what should I do ?
DocumentSnapshot.datais an object, not an array, although the[ ]syntax can often work. - LeadDreamer