Firestore在更新数组时不会存储两个相同的值 [英] Firestore doesn't store two identical values while updating an array

查看:74
本文介绍了Firestore在更新数组时不会存储两个相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用flutter,当使用两个或多个连续相同的值更新数组时,firestore仅添加一个。

Using flutter, when updating an array with 2 or more consecutive identical values, firestore only adds one.

示例:

void updateValue() {
var now = [new DateTime.now()];
int pain =5;
 Firestore.instance.collection('Patient').document('bC2ML7LaQ1fWGOl37ZUq').collection('Symptom').document('2H6e99Bvrz6h0HBzgnyg')
            .updateData({ 'nauseaLevel' : FieldValue.arrayUnion([pain]), 'nauseaTime':FieldValue.arrayUnion(now)});
          }

执行两次此功能时,firestore中的数组 nauseaLevel将仅添加一个痛苦值,而忽略第二个。

When executing this function 2 times, the array "nauseaLevel" in firestore will only add one "pain" value and ignore the second.

由于值不同,数组nauseaTime可以按预期工作。

the array nauseaTime works as expected as the values are different.

推荐答案

根据有关更新数组中的元素


arrayUnion()将元素添加到数组中,但仅添加不存在的元素。

arrayUnion() adds elements to an array but only elements not already present.

因此,您无法在使用 arrayUnion()函数在Cloud Firestore中创建数组。

So there is no way you can add duplicate elements within an array in Cloud Firestore using arrayUnion() function.

不过,为了避免误解,您可以添加重复的元素在数组中,但仅当您阅读了整个数组客户端时,才进行添加(对于所有dup licates),然后将其写回数据库。

To avoid a misunderstanding, however, you can add duplicate elements within an array but only if you read the entire array client side, do the additions (with all duplicates) and then write it back to the database.

这篇关于Firestore在更新数组时不会存储两个相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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