How can I append a new Map
type in firestore?
void addUser() async {
final us = _firestore.collection("users").doc(_search.text);
us.update({
"requests": (
{_auth.currentUser?.email: rep}
),
});
}
Am using this method but the requests field in my firestore overwrites the previous one I want it to be appended. Any idea?
2
Answers
The
update()
method will always overwrite your previous field with the new one, so achieving this with one operation using theupdate()
is not possible, however, you can always get the current field from the document, then update its value, then save it again in the document like this:But you should use this after being aware that this method will make two operations in your database, a
get()
andupdate()
.If you want to record multiple values, an array is an appropriate type. So, you could use the
.arrayUnion
method to record multiple entries.