I have cloud Firestore with one Collection , this collection has many documents and documents have many fields
I use stream to get data from the collection and it was work fine but I add some conditions that make my code complex and I didn’t understand how to solve it….
if (snapshot.connectionState == ConnectionState.done) {
DocumentSnapshot doc = snapshot.data!.docs.map((e) =>
e.data()) as DocumentSnapshot<Object?>;
Map<String, dynamic> data = doc as Map<String, dynamic>;
print(data.toString());
}
I want to know how to read:
- documents in that collection
- field in this documents
this my code
Widget build(BuildContext context) {
final auth = Provider.of<AuthBase>(context,listen: false);
return StreamBuilder<QuerySnapshot>(
stream: auth.streamStateChanges()
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
}
if (snapshot.connectionState == ConnectionState.active) {
if (snapshot.connectionState == ConnectionState.done) {
print(snapshot.data?.docs.map((e) => e.data()));//???????
}
}
return Container();
});
what should I replace this code I want when ConnectionState.done to return a list of (documents , field) from my collection
*===> final _database = FirebaseFirestore.instance.collection('userInfo');
*===> Stream<QuerySnapshot> streamStateChanges() => _database.snapshots();
2
Answers
here provider
this home screen
read field in the specific document
The document Id of each document must be saved in its field so you can read the specific document data when you get all documents
example:
for get specific field value
your stream should be like this
fore read data like this
///////////////////////////////////////////////////////////////////////
if your stream is return like this:
it returns a list of documents so to reach for each document you should use it like that