Cloud Firestore query to get doc ID, Flutter

Manal

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

osaxma

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Query firestore database for document id

From Java

How to get documents with specific id in different collections efficiently in Cloud Firestore?

From Java

Unable to get data from Firestore using Flutter

From Java

Query a single document from Firestore in Flutter (cloud_firestore Plugin)

From Java

Difference between get() and onSnapshot() in Cloud Firestore

From Java

Flutter Firestore add new document with Custom ID

From Java

FieldValue arrayUnion and Cloud FireStore with Flutter

From Dev

Get all doc data from Cloudant as opposed to id, key, value

From Dev

Using "array-contains" Query for Cloud Firestore Social Media Structure

From Dev

Flutter - Update values of maps and arrays in Cloud firestore

From Dev

Flutter cloud_firestore persistence is not working even if it is enabled

From Dev

Display images of Firebase Storage with a Firestore query to get image url in Flutter

From Dev

Arranging icons from cloud firestore in flutter

From Dev

How to get subcollections and your documents in Firestore/Flutter

From Dev

Android Cloud Firestore document fields query

From Dev

FetchData From Cloud FireStore Firebase in flutter (dart)

From Dev

Firestore get subcollection from where query

From Dev

How to make query Cloud Firestore by values

From Dev

How to get internal doc id set by lucene

From Dev

SQL query to get an id

From Dev

Get/add Document in Cloud functions using Firestore

From Dev

Cloud Firestore query result set is empty

From Dev

Cloud FireStore: Retrieve 1 document with query

From Dev

How to firestore query on nested doc. Firebase Nodejs

From Dev

Cloud Firestore: Query to check if a username exists

From Dev

Compare an id to an array in Cloud firestore with Angular

From Dev

Convert SQL query to Cloud Firestore on Android development

From Dev

Firestore Cloud Function Query returning value after database query?

From Dev

Is It Possible to Have a Slow Query In Cloud Firestore?

Related Related

  1. 1

    Query firestore database for document id

  2. 2

    How to get documents with specific id in different collections efficiently in Cloud Firestore?

  3. 3

    Unable to get data from Firestore using Flutter

  4. 4

    Query a single document from Firestore in Flutter (cloud_firestore Plugin)

  5. 5

    Difference between get() and onSnapshot() in Cloud Firestore

  6. 6

    Flutter Firestore add new document with Custom ID

  7. 7

    FieldValue arrayUnion and Cloud FireStore with Flutter

  8. 8

    Get all doc data from Cloudant as opposed to id, key, value

  9. 9

    Using "array-contains" Query for Cloud Firestore Social Media Structure

  10. 10

    Flutter - Update values of maps and arrays in Cloud firestore

  11. 11

    Flutter cloud_firestore persistence is not working even if it is enabled

  12. 12

    Display images of Firebase Storage with a Firestore query to get image url in Flutter

  13. 13

    Arranging icons from cloud firestore in flutter

  14. 14

    How to get subcollections and your documents in Firestore/Flutter

  15. 15

    Android Cloud Firestore document fields query

  16. 16

    FetchData From Cloud FireStore Firebase in flutter (dart)

  17. 17

    Firestore get subcollection from where query

  18. 18

    How to make query Cloud Firestore by values

  19. 19

    How to get internal doc id set by lucene

  20. 20

    SQL query to get an id

  21. 21

    Get/add Document in Cloud functions using Firestore

  22. 22

    Cloud Firestore query result set is empty

  23. 23

    Cloud FireStore: Retrieve 1 document with query

  24. 24

    How to firestore query on nested doc. Firebase Nodejs

  25. 25

    Cloud Firestore: Query to check if a username exists

  26. 26

    Compare an id to an array in Cloud firestore with Angular

  27. 27

    Convert SQL query to Cloud Firestore on Android development

  28. 28

    Firestore Cloud Function Query returning value after database query?

  29. 29

    Is It Possible to Have a Slow Query In Cloud Firestore?

HotTag

Archive