Field.array union for firebase in flutter not saving list elements with same value

huangapple go评论61阅读模式
英文:

Field.array union for firebase in flutter not saving list elements with same value

问题

I understand. Here is the translated content:

我正在使用Firebase数据库为我用Flutter开发的移动应用程序。我想在这个数据库中添加一个新数组。我将我的数据添加到一个列表中。这些数据包含相同的值。当我使用`FieldValue.arrayUnion(stats)`代码时,只有具有不同值的元素才会被添加。

我创建的列表包含以下值;
\[0-0-0-0 , 0-0-0-0 ,0-0-0-0 ,0-0-0-0 \].
保存在Firebase数据库中的值只是0-0-0-0

当值不同时,Firebase数据库会进行保存

当我通过Firebase网站添加时,没有问题,但当我用Flutter做时,我遇到了上述的保存问题。你能帮我吗?谢谢。

```dart
for(int i = 0; i < 3;i++)
{
    stats.add("0-0-0-0");
}

FirebaseFirestore.instance
    .collection("xxxx")
    .doc(auth.currentUser!.uid)
    .collection("xxxxx")
    .doc(xxxxx)
    .set({
  "Lang_1": FieldValue.arrayUnion(xxxxx),
  "Lang_2": FieldValue.arrayUnion(xxxxx),
  "Length": counter,
  "Stats": FieldValue.arrayUnion(stats),
});

我的解决方案。

stats.clear();
for (int i = 0; i < 3; i++) {
    stats.add("0-0-0-0");
}
final docData = {
    "x": x,
    "x": x,
    "x": x,
    "Stats": stats,
};

FirebaseFirestore.instance
    .collection("x")
    .doc(x)
    .collection("x")
    .doc(x)
    .set(
    docData,
);
英文:

I am using Firebase Database for my mobile application that I developed with Flutter. I want to add a new array to this database. I am adding my data to a list. These data contain the same values. When I use the FieldValue.arrayUnion(stats) code, only elements with different values are added.

The list I made contains the following values;
[0-0-0-0 , 0-0-0-0 ,0-0-0-0 ,0-0-0-0 ].
value of saved firebase is just 0-0-0-0.

Firebase database saves when the values are different.

When I add through the firebase site, there is no problem, but when I do it with flutter, I encounter the above mentioned saving problem. Can you help me? Thanks.

    for(int i = 0; i < 3;i++)
    {
        stats.add("0-0-0-0");
    }

      FirebaseFirestore.instance
          .collection("xxxx")
          .doc(auth.currentUser!.uid)
          .collection("xxxxx")
          .doc(xxxxx)
          .set({
        "Lang_1": FieldValue.arrayUnion(xxxxx),
        "Lang_2": FieldValue.arrayUnion(xxxxx),
        "Length": counter,
        "Stats": FieldValue.arrayUnion(stats),
      });

my solution.

  stats.clear();
  for (int i = 0; i < 3; i++) {
    stats.add("0-0-0-0");
  }
  final docData = {
    "x": x,
    "x": x,
    "x": x,
    "Stats": stats,
  };

  FirebaseFirestore.instance
      .collection("x")
      .doc(x)
      .collection("x")
      .doc(x)
      .set(
        docData,
      );

答案1

得分: 0

这是预期的行为:arrayUnion 将数组内容视为集合,并且只有在该项尚未存在时才添加。

没有arrayAdd操作符,所以如果要添加重复项,您需要:

  1. 在您的应用程序代码中读取文档
  2. 将项目添加到数组中
  3. 将整个数组写回数据库。

另请参阅:

英文:

That's the expected behavior: arrayUnion treats the array contents as a set, and only adds the item if it isn't already present.

There is no arrayAdd operator, so if you want to add a duplication, you will have to:

  1. Read the document in your application code
  2. Add the item to the array
  3. Write the entire array back to the database.

Also see:

huangapple
  • 本文由 发表于 2023年5月21日 23:04:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定