Reputation: 891
I am learning flutter and creating my first mobile app. I have learned a lot from many of you during this process.
I have a new question. My app has multiple documents in a certain collection in Firestore. I want to access the data from one document but I don't know the document name. How can I access a document in flutter when I don't know the document name?
Upvotes: 0
Views: 116
Reputation: 3499
You don't query off of DocumentReferences (one document); you query off of CollectionReferences (sets of documents). A DocumentSnapshot is just a single document. I don't use flutter (yet; give me a week or two), so I can't help you with the code syntax.
Upvotes: 0
Reputation: 2984
You don't need the document name to access the document, but the document has to have a unique field so you can filter the collection out and access that document.
final firestore = FirebaseFirestore.instance
final query = firestore.collection('myCollection').where('uniqueField', isEqualTo: uniqueValue);
final docs = (await query.get()).docs;
final document = docs.first.data();
Please note, the last line of code assumes that you're sure there's only one document. If that's not the case, you've to check the length of the docs and do extra checking after that to filter the docs out and find your document.
Also, if you end up filtering documents on the flutter side and you're not sure about how many documents will be returned, it'll be a good idea to limit the query by adding additional where
filters and using limit
method at the end of the query.
Upvotes: 1