Cloud Firestore query to get doc ID, Flutter
Solution 1
You don't have the document ID available in your function, and there's no way to know it without keeping the documentID
once you retrieve the data and pass it to the function. Or you have to query the document again by filtering through a unique combination of fields (i.e., userId
and medID
).
The simplest option is to include the document ID in every document under medicine upon creation. So you can update the document by ID directly under your onPressed
function given that you've access to the document in that function.
If you decided to do that, you can include the document ID upon creation as follows:
// get a new reference for a document in the medicine collection
final ref = FirebaseFirestore.instance.collection('medicine').doc();
// upload the data and include docID in it
await ref.set({
'docID': ref.id,
'medID': medID,
// rest of the data
});
This way when you retrieve the medicines
you always have the documentID handy inside each document. And you can update it directly by just placing the docID where you've **????????**
.
Solution 2
In order to find a specific document in a given collection you have two solutions:
- You know its ID, which is not your case;
- You can build a query that will uniquely identify the document. For example, in your case, based on the fields
userId
ormedId
or any other field or a combination of several fields.
With the info shared in your question we cannot define which query you should run to find the desired document. If you add more details in your question it might be possible to answer more precisely.
Update following your comment
You first need to get the document via the query, then get its DocumentReference
, then update it, as follows:
QuerySnapshot querySnap = await FirebaseFirestore.instance.collection('medicine').where('userId', isEqualTo: user.uid).get();
QueryDocumentSnapshot doc = querySnap.docs[0]; // Assumption: the query returns only one document, THE doc you are looking for.
DocumentReference docRef = doc.reference;
await docRef.update(...);
Manal
Updated on December 28, 2022Comments
-
Manal over 1 year
I develop an application that remind elderly about their medication time, and know I want to enable them to updater their medication and store any changes they have. that's my code to update medication name:
onPreesed: () async { final updatedMedcName1 = await FirebaseFirestore.instance .collection('medicine') .doc(**????????**) .update({'medicationName': updatedMedcName}); // Navigator.of(context).pop(); },
but I don't know how can I get the doc id, which is below:(underlined in red)
for field
medId
I got the id using code:FirebaseFirestore.instance.collection('medicine').doc().id,
but it's not the same like id underlined in red
-
Manal about 3 yearsI tried to put Where condition, for example like this: (FirebaseFirestore.instance .collection('medicine') .where('userId', isEqualTo: user.uid)),,, but I cant use 'update' when I use where condition
-
Renaud Tarnec about 3 yearsYou first need to get the document via the query, then get its
DocumentReference
, then update it. -
Manal about 3 yearsI put my code in answers, you can check it
-
Renaud Tarnec about 3 yearsPlease don't add your code as an answer: you can edit your initial question by clicking on the "Edit" link at the bottom of your question.
-
Manal about 3 yearsThank you for trying to solve my problem, I really appreciate that and finally my problem is solved