如何在Flutter cloud_firestore中更新数组值? [英] How do update array values in Flutter cloud_firestore?

查看:98
本文介绍了如何在Flutter cloud_firestore中更新数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有数组字段的文档。

I have a document with an array field in it.

如何更新数组?

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

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
                       })
    })

我无法找到Flutter的任何替代品。我该怎么办?

I cannot find any alternative with Flutter.. how can I do?

推荐答案

我为您提供的解决方案是使用事务时,但无论如何,它的工作原理都相同……

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,
  },
);

这篇关于如何在Flutter cloud_firestore中更新数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆