Georgios
Georgios

Reputation: 1037

Add element into an existing Map in Cloud Firestore

Through Flutter I want to add a Map element in Firestore.

Instead of fetching the whole map and replacing it with the added element is it possible to directly add a new key and value?

Example:

Map = { '2020-05-21' : true } -- Add --> Map = { '2020-05-21' : true, '2020-05-22': true }

This is how I tried to do this but without any success.

return collection.document('id').updateData({'map.$date' : true});

enter image description here

Upvotes: 1

Views: 832

Answers (1)

CopsOnRoad
CopsOnRoad

Reputation: 267404

Use this approach:

final ref = Firestore.instance.document('collection_id/document_id');

await ref.updateData({
  'field.key2': 'value2',
});

Since I am not having an idea about how your data looks like on server, I tested this one and it worked.


Edit:

// initial work
var ref = Firestore.instance.document('Employees/54G...Naf');
var initialData = {
  'date_1': '1',
};
await ref.setData({
  'busy_map': initialData,
});

// updated work
var updatedData = {
  'busy_map.date_2': '2',
};
await ref.updateData(updatedData);

Upvotes: 2

Related Questions