如何在Flutter cloud_firestore中更新数组值?

时间:2018-10-09 19:44:15

标签: dart flutter google-cloud-firestore

我有一个文档,其中包含数组字段。

如何更新阵列?

在firebase函数中,使用打字稿,我做了类似的事情:

admin.firestore()
    .collection('friendships')
    .doc(caller.data["uid"])
    .update({
        friends: admin.firestore.FieldValue
            .arrayUnion({
                          friendDisplayName: snapshot.data["friendDisplayName"],
                          friendUid: snapshot.ref
                       })
    })

我无法在Flutter上找到其他选择。我该怎么办?

2 个答案:

答案 0 :(得分:0)

类似这样的东西

firestore.instance.
    .collection('friendships')
    .document(caller.data["uid"])
    .updateData({
  friends: FieldValue.arrayUnion({
    friendDisplayName: snapshot.data["friendDisplayName"],
    friendUid: snapshot.ref
  })
});

答案 1 :(得分:0)

我为您提供的解决方案是使用“事务”时,但是不管怎样,它的工作原理都是一样的... List<dynamic> list = List.from(documentSnapshot.data['uids']); list.add(uid); await documentTransaction.update( postRef, <String, dynamic>{ 'counter': documentSnapshot.data['counter'] + 1, 'uids': list, }, );