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 debugcn@gmail.com to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

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

From Dev

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

From Java

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

From Java

FieldValue arrayUnion and Cloud FireStore with Flutter

From Java

Query firestore database for document id

From Dev

Flutter - Update values of maps and arrays in Cloud firestore

From Dev

Arranging icons from cloud firestore in flutter

From Dev

FetchData From Cloud FireStore Firebase in flutter (dart)

From Dev

Android Cloud Firestore document fields 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 Java

Flutter Firestore add new document with Custom ID

From Java

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

From Java

Unable to get data from Firestore using Flutter

From Dev

How to get subcollections and your documents in Firestore/Flutter

From Dev

Firestore get subcollection from where query

From Dev

Flutter cloud_firestore persistence is not working even if it is enabled

From Dev

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

From Dev

Can I query and retrieve Documents' field values in Subcollection in Cloud Firestore?

From Dev

Get the Row ID dynamically in the query

From Dev

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

From Dev

Parse cloud query to get object with closest GeoPoint

From Java

How to get last Firestore ID document

From Dev

Problem get ID (Angular Material - Firestore)

From Dev

Flutter: Cannot use Firestore.instance even after importing the package(cloud_firestore)

From Dev

Flutter: Cannot use Firestore.instance even after importing the package(cloud_firestore)

From Java

How to get a count of number of documents in a collection with Cloud Firestore

From Java

How to get data from cloud firestore then filter it, map it then return it?

Related Related

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

    FieldValue arrayUnion and Cloud FireStore with Flutter

  5. 5

    Query firestore database for document id

  6. 6

    Flutter - Update values of maps and arrays in Cloud firestore

  7. 7

    Arranging icons from cloud firestore in flutter

  8. 8

    FetchData From Cloud FireStore Firebase in flutter (dart)

  9. 9

    Android Cloud Firestore document fields query

  10. 10

    How to make query Cloud Firestore by values

  11. 11

    How to get internal doc id set by lucene

  12. 12

    SQL query to get an id

  13. 13

    Flutter Firestore add new document with Custom ID

  14. 14

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

  15. 15

    Unable to get data from Firestore using Flutter

  16. 16

    How to get subcollections and your documents in Firestore/Flutter

  17. 17

    Firestore get subcollection from where query

  18. 18

    Flutter cloud_firestore persistence is not working even if it is enabled

  19. 19

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

  20. 20

    Can I query and retrieve Documents' field values in Subcollection in Cloud Firestore?

  21. 21

    Get the Row ID dynamically in the query

  22. 22

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

  23. 23

    Parse cloud query to get object with closest GeoPoint

  24. 24

    How to get last Firestore ID document

  25. 25

    Problem get ID (Angular Material - Firestore)

  26. 26

    Flutter: Cannot use Firestore.instance even after importing the package(cloud_firestore)

  27. 27

    Flutter: Cannot use Firestore.instance even after importing the package(cloud_firestore)

  28. 28

    How to get a count of number of documents in a collection with Cloud Firestore

  29. 29

    How to get data from cloud firestore then filter it, map it then return it?

HotTag

Archive