I have a document with an array field in it.
How can I update the array?
In firebase functions, with typescript, I did something like this:
admin.firestore()
.collection('friendships')
.doc(caller.data["uid"])
.update({
friends: admin.firestore.FieldValue
.arrayUnion({
friendDisplayName: snapshot.data["friendDisplayName"],
friendUid: snapshot.ref
})
})
I cannot find any alternative with Flutter.. how can I do?
Something like this
firestore.instance.
.collection('friendships')
.document(caller.data["uid"])
.updateData({
friends: FieldValue.arrayUnion({
friendDisplayName: snapshot.data["friendDisplayName"],
friendUid: snapshot.ref
})
});
The solution I give you is when you use Transactions, but regardless of that, it works the same...
List<dynamic> list = List.from(documentSnapshot.data['uids']);
list.add(uid);
await documentTransaction.update(
postRef,
<String, dynamic>{
'counter': documentSnapshot.data['counter'] + 1,
'uids': list,
},
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With