Reputation: 145
I am building a flutter app which essentially tracks the user's collection of trading cards. I'm using Firestore as my database. I'm finding a lot of content online on how to cycle through a collection and display it as a list, but I want to only access a particular user's data. My database currently has one collection called User_data. Within this collection, there is a document for each user, identified by the uid for the user, given upon registration. Within each user document, there is the user's data, such as their name, email, as well as a list of trading cards they own.
I want to make a page, which shows the list of cards owned by the user who is currently logged in. Most solutions I have found online create lists from ALL documents in a collection. I am trying to use StreamBuilder as follows:
StreamBuilder(
stream: FirebaseFirestore.instance.collection('user_data').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
} else {
However, the stream in the above solution gives me all documents, not the document of the current user. Can I do something like:
FirebaseFirestore.instance.collection('user_data').snapshots(uid)
where uid is the unique ID of the current user, so that I can only get their data?
Upvotes: 1
Views: 789
Reputation: 1861
you can actually do so like this:
FirebaseFirestore.instance
.doc(yourDocId)
.snapshots()
this would give you a stream for your document.
Upvotes: 3