I want to add Map<> data to my firestore database with code :
Map<String, Object> prop = {
'read' : true,
'vote_left' : false,
'vote_right' : false,
};
Map<String, Object> attended_topic =
{
received_id:prop
};
FirebaseFirestore.instance.collection('userinfo').doc(user_now!.uid)
.update({"attended_topic": FieldValue.arrayUnion([attended_topic])});
What I expected is this.
attended_topic
topicId_0
read : true
vote_left : false
vote_right : false
But I got something unexpected.
attended_topic
0
topicId_0
read : true
vote_left : false
vote_right : false
I never expected that new category ‘0’ appearing and donot know why. Since the atabase has other properties so I thought using update()
rather than set()
is adequate. Please somebody tell my why this happens.
2
Answers
I solved this problem referring Peter's solution and changing it slightly.
From the docs
FieldValue.arrayUnion
adds elements to an array but only elements not already present.So
{"a": FieldValue.arrayUnion([b])}
addsb
variable to theArray
a
.To solve your problem, just remove
FieldValue
as shown below.