3

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

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

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

widget.head: widget.id, how can i use the template string instead of widget.id
If you are asking about string interpolation in dart, then it same as assigning above: widget.head: "Key: ${widget.id}"
No, I mean in js.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.