Andrew Hsu
Andrew Hsu

Reputation: 61

In Firebase how do we add more fields to a document through flutter?

I'm not sure how to add an extra field to a firestore document through code. I do not want to create new fields through firebase console, since these field names will be dynamic and be used for searching. EX: if a field does "NOT" exist, add to result. (Since firestore does not allow negation queries.

Upvotes: 5

Views: 10362

Answers (3)

hackrhive
hackrhive

Reputation: 153

In Cloud_Firestore#0.14.0, there has been many changes.

"setData/set now supports SetOptions to merge data/fields (previously this accepted a Map)"

Now, for this you need to do it as follows:

FirebaseFirestore.instance.collection('collectionName')
  .doc('docID')
  .set({
  'field': 'value'
},SetOptions(merge: true)).then((value){
  //Do your stuff.
});

Upvotes: 15

tsvillain
tsvillain

Reputation: 71

firestore.instance.collection("collectionName").document("documentID").setData({field : value }, merge: true).then((onValue){//your other code}

This Code will work. Use setData and use merge: true as shown in the above code snippet.

Upvotes: 2

user321553125
user321553125

Reputation: 3216

firestore.instance.collection('yourDbCollection').doc('ifYourIdCostumized').update({
            field: newItem
        })
            .then(function () {
                console.log("Document successfully updated!");
            }).catch(function (error) {
                console.error("Error removing document: ", error);

            });

Upvotes: 3

Related Questions