how to update field on document on cloud firestore using Flutter?

Viewed 19032

I tried to update my firestore database field.

  Future<void> approveJob(String categoryId) {

comment line is updated on database. But I hard code uid. Is it possible to get uid without store?

   //return _db.collection('jobs').document('25FgSmfySbhEPe1z539T').updateData({'isApproved':true});


   return _db
        .collection('jobs')
        .where("categoryId", isEqualTo: categoryId)
        .getDocuments()
        .then((v) {
          try{
            v.documents[0].data.update('isApproved', (bool) => true,ifAbsent: ()=>true);

// No Errors. But not updating

         }catch(e){
            print(e);
          }
    });
  }
5 Answers

===December 2020===

There are two methods for updating Firestore documents in Flutter:

  1. set() - Sets data on the document, overwriting any existing data. If the document does not yet exist, it will be created.
  2. update() - Updates data on the document. Data will be merged with any existing document data. If no document exists yet, the update will fail.

So, for updating the exact field in the existing document you can use

FirebaseFirestore.instance.collection('collection_name').doc('document_id').update({'field_name': 'Some new data'});
  • To update a value in the document:

    var collection = FirebaseFirestore.instance.collection('collection');
    collection 
        .doc('doc_id') 
        .update({'key' : 'value'}) // <-- Updated data
        .then((_) => print('Success'))
        .catchError((error) => print('Failed: $error'));
    
  • To update a nested value in the document.

    var collection = FirebaseFirestore.instance.collection('collection');
    collection 
        .doc('doc_id')
        .update({'key.foo.bar' : 'nested_value'}) // <-- Nested value
        .then((_) => print('Success'))
        .catchError((error) => print('Failed: $error'));
    
  • To add a new value to the existing document.

    var collection = FirebaseFirestore.instance.collection('collection');
    collection
        .doc('doc_id')
        .set(yourData, SetOptions(merge: true)); // <-- Set merge to true.
    

First of all, you need to identify and locate the field you want to modify.

final code = Code.fromSnapshot(document);

FirebaseFirestore.instance.collection('collection_Name').doc('doc_Name').collection('collection_Name').doc(code.documentId).update({'redeem': true});

In this case code.documentId code is the instance that contains the snapshot. By using this it's possible to access the 'identifier' documentId; and then the field redeem is changed to true,

FirebaseFirestore.instance.collection('groups').doc(chatId).collection('groupChat')
        .doc(chatId).update({
      'lastMessage': messageTextController.text.toString(),
      'lastMessageSendBy' : currentUser,
      'lastMessageTime' : Timestamp.now(),
    });

In order to update a field in a document you just have to follow this documentation.

Regarding how to get the documentID, you could do it as described here

Also bear in mind that you can update data with transactions.

Let me know if this was helpful.

Related