You*_*ehi 3 arrays firebase flutter google-cloud-firestore
使用 flutter,当更新具有 2 个或更多连续相同值的数组时,firestore 只会添加一个。
例子 :
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)});
}
Run Code Online (Sandbox Code Playgroud)
执行此函数 2 次时,firestore 中的数组“nauseaLevel”只会添加一个“痛苦”值而忽略第二个。
由于值不同,数组nauseTime 按预期工作。
根据关于更新数组中元素的官方文档:
arrayUnion()将元素添加到数组,但只添加不存在的元素。
因此,您无法使用arrayUnion()函数在 Cloud Firestore 中的数组中添加重复元素。
但是,为了避免误解,您可以在数组中添加重复元素,但前提是您读取整个数组客户端,进行添加(包含所有重复项),然后将其写回数据库。