简体   繁体   English

Firebase Flutter arrayUnion 什么都不做

[英]Firebase Flutter arrayUnion not doing anything

I'm trying to add a specific id to a array of children, but everytime I run my code it simply does nothing and I'm not sure why.我试图将一个特定的 id 添加到一个子数组中,但每次我运行我的代码时它什么都不做,我不确定为什么。

 Future addChild() async {
    await FirebaseFirestore.instance.collection('users').doc(id).update({
      'children': FieldValue.arrayUnion([cid])
    });
  }

This is the current Firestore Database I have:这是我当前拥有的 Firestore 数据库:

在此处输入图像描述

you need to add one key which only store list of ids i think the mistak your code is you add or update directly in json so you jave to try like this way and for example hear i am add "cidList" key in "children".您需要添加一个仅存储 ID 列表的密钥,我认为您的代码是您直接在 json 中添加或更新的错误,因此您需要尝试这种方式,例如听到我在“儿童”中添加“cidList”密钥。 in my project, I m use arrayUnion in "usersLikesMe" keys in add to users id.在我的项目中,我在“usersLikesMe”键中使用 arrayUnion 添加到用户 ID。

await FirebaseFirestore.instance.collection('users').doc(id).set({
  'children.cidList': FieldValue.arrayUnion([cid])
},SetOptions(merge: true));

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM