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
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 **????????**
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments