Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Firestore query to get doc ID, Flutter

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) enter image description here

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

like image 765
Manal Avatar asked Nov 17 '25 09:11

Manal


2 Answers

In order to find a specific document in a given collection you have two solutions:

  1. You know its ID, which is not your case;
  2. You can build a query that will uniquely identify the document. For example, in your case, based on the fields userId or medId 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(...);
like image 136
Renaud Tarnec Avatar answered Nov 19 '25 01:11

Renaud Tarnec


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 **????????**.

like image 26
osaxma Avatar answered Nov 19 '25 00:11

osaxma