Ali Coder
Ali Coder

Reputation: 323

How to add map inside document in firestore using flutter?

I am using map inside array in firestore document. The problem is that when i am adding maps with new values then instead of increasing the size of array it is replacing new map with current map. My code is look like this:

Map<String, dynamic> map = {'name': "Asad"};

RaisedButton(
    child: Text("press me"),
    onPressed: () {
      Firestore.instance.collection('Shops').document('2').setData({
        'name': 'Ali',
        'id': '3',
        'array': FieldValue.arrayUnion([map])
      });
    },
  ),

And my firestore db looks like this:

enter image description here How to increase the size of array or you can say how to add new element inside array instead of replacing it.Thanks in advance.

Upvotes: 0

Views: 352

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

If you want to update a document instead of overwriting its content, you should use updateData() instead of setData(). Either that, or pass {merge: true} to setData() as the second argument.

Upvotes: 2

Related Questions