I'm trying to get DocumentSnapshot from collection
this my code
Stream<QuerySnapshot> streamState() => collectionRef.snapshots();
return StreamBuilder<QuerySnapshot>(
stream: auth.streamState(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
if (snapshot.hasData){
DocumentSnapshot doc = snapshot.data!.docs.elementAt(0);
print(doc.id);
}
return Container(
color: Colors.white,
);
}
);
it work fine but as you see I use elementAt(0) I want to get doc by its id
I try with docs.where but fail.
CodePudding user response:
If you know the ID of the document you want to listen to, you can do:
Stream<QuerySnapshot> streamState() => collectionRef.doc("the document ID").snapshots();
And then the StreamBuilder
becomes:
return StreamBuilder<DocumentSnapshot>(
stream: auth.streamState(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> asyncSnapshot){
if (asyncSnapshot.hasData){
DocumentSnapshot doc = asyncSnapshot.data!;
print(doc.id);
}
return Container(
color: Colors.white,
);
}
);
CodePudding user response:
The data that you are getting from auth.streamState() is I suppose about authentication? You should get it by id there probably