Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to add new data to existing map in firebase flutter

here is the firebase document data

enter image description here

I want to add a new map value to the purchased-id. this is the current code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

FirebaseFirestore.instance.collection('users').doc(userID).set({
  'purchased-id': {
    widget.head: widget.id,
  }
});               

but with this code the map value is replaced,
how do I add another map value to the existing map

like this

enter image description here

>Solution :

I think need merge: true to update the map with its new value.

FirebaseFirestore.instance.collection('users').doc(userID).set({
  'purchased-id': {
    widget.head: widget.id,
  }
}, SetOptions(merge: true));

Reference: https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects

Also to differentiate with .set and .update, have a look at this answer: https://stackoverflow.com/a/47554197/11566161

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading