英文:
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
操作符,所以如果要添加重复项,您需要:
- 在您的应用程序代码中读取文档
- 将项目添加到数组中
- 将整个数组写回数据库。
另请参阅:
- https://stackoverflow.com/questions/76113338/flutter-firestore-add-value-to-array-that-already-exists/76113617#76113617
- https://stackoverflow.com/questions/57423352/how-do-i-add-equal-elements-to-an-array-in-firestore-in-flutter
- https://stackoverflow.com/questions/56110491/firestore-doesnt-store-two-identical-values-while-updating-an-array
英文:
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:
- Read the document in your application code
- Add the item to the array
- Write the entire array back to the database.
Also see:
- https://stackoverflow.com/questions/76113338/flutter-firestore-add-value-to-array-that-already-exists/76113617#76113617
- https://stackoverflow.com/questions/57423352/how-do-i-add-equal-elements-to-an-array-in-firestore-in-flutter
- https://stackoverflow.com/questions/56110491/firestore-doesnt-store-two-identical-values-while-updating-an-array
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论