如何更新 firebase 数组中的地图数据? (扑)
How can I update map data that's in a array in firebase? (Flutter)
我正在为一个项目使用 flutter web 和 firebase,但遇到了一个问题。我正在尝试更新 firestore 中数组中的地图。
使用这个:
var val = [];
val.add({'groupUID': groupUID, 'inviteStatus': status});
var userInviteUID;
await users
.document(uid)
.get()
.then((value) => userInviteUID = value.data['inviteUID']);
await invites
.document(userInviteUID)
.updateData({'invites': FieldValue.arrayUnion(val)});
我得到了这个结果:
firestore structure
我要做的就是把地图上的1改成2。我认为它会更新,因为它具有相同的值,但它只是将它添加到数组中。
我查看了堆栈,发现了一些方法可以做到这一点,例如复制整个数组并在需要的地方更改它,然后再将其添加回去。
但我想知道是否有办法通过对我的代码进行一些修改来避免这种情况。如果有更好的结构我应该使用,也请告诉我。感谢您的帮助!
更新:
var ref = invites.document(userData.inviteUID);
ref.get().then((value) async {
var invitesList = value.data['invites'];
switch (status) {
case 1:
break;
case 2:
var index;
invitesList.asMap().forEach((key, value) {
if (value['groupUID'] == groupUID) index = key;
});
invitesList.removeAt(index);
await invites
.document(userData.inviteUID)
.updateData({'invites': FieldValue.arrayUnion(invitesList)});
break;
default:
}
所以我查看了一些打印语句,发现删除了具有匹配组 uid 的元素,但是查看 firebase,数组没有被覆盖任何东西……有什么想法吗?
最终更新:
var ref = invites.document(userData.inviteUID);
ref.get().then((value) async {
var invitesList = value.data['invites'];
switch (status) {
case 1:
break;
case 2:
var index;
invitesList.asMap().forEach((key, value) {
if (value['groupUID'] == groupUID) index = key;
});
invitesList.removeAt(index);
await invites
.document(userData.inviteUID)
.setData({'invites': FieldValue.arrayUnion(invitesList)});
break;
default:
}
通过将 updateData 更改为 setData 来修复它。
I looked around on stack and saw some ways to do it like copying the entire array and changing it where I need to, then adding it back.
这正是您应该如何修改 Firestore 文档中的数组内容。 Firestore 不支持按索引更新数组元素。
setData
如果文档不存在则创建一个新文档,但如果文档存在,数据将被覆盖。为防止这种情况发生,如果您希望附加数据,可以使用 SetOptions(merge: true)
:
set(someData, SetOptions(merge: true))
你也可以使用update
方法并向其提供更新后的数据,伪代码如下所示:
List<Map<String, dynamic>> updatedList = [...];
Map<String, dynamic> updatedData = {
'existing_map': updatedList,
};
var collection = FirebaseFirestore.instance.collection('collection');
collection
.doc('doc_id')
.update(updatedData);
我正在为一个项目使用 flutter web 和 firebase,但遇到了一个问题。我正在尝试更新 firestore 中数组中的地图。
使用这个:
var val = [];
val.add({'groupUID': groupUID, 'inviteStatus': status});
var userInviteUID;
await users
.document(uid)
.get()
.then((value) => userInviteUID = value.data['inviteUID']);
await invites
.document(userInviteUID)
.updateData({'invites': FieldValue.arrayUnion(val)});
我得到了这个结果: firestore structure
我要做的就是把地图上的1改成2。我认为它会更新,因为它具有相同的值,但它只是将它添加到数组中。
我查看了堆栈,发现了一些方法可以做到这一点,例如复制整个数组并在需要的地方更改它,然后再将其添加回去。
但我想知道是否有办法通过对我的代码进行一些修改来避免这种情况。如果有更好的结构我应该使用,也请告诉我。感谢您的帮助!
更新:
var ref = invites.document(userData.inviteUID);
ref.get().then((value) async {
var invitesList = value.data['invites'];
switch (status) {
case 1:
break;
case 2:
var index;
invitesList.asMap().forEach((key, value) {
if (value['groupUID'] == groupUID) index = key;
});
invitesList.removeAt(index);
await invites
.document(userData.inviteUID)
.updateData({'invites': FieldValue.arrayUnion(invitesList)});
break;
default:
}
所以我查看了一些打印语句,发现删除了具有匹配组 uid 的元素,但是查看 firebase,数组没有被覆盖任何东西……有什么想法吗?
最终更新:
var ref = invites.document(userData.inviteUID);
ref.get().then((value) async {
var invitesList = value.data['invites'];
switch (status) {
case 1:
break;
case 2:
var index;
invitesList.asMap().forEach((key, value) {
if (value['groupUID'] == groupUID) index = key;
});
invitesList.removeAt(index);
await invites
.document(userData.inviteUID)
.setData({'invites': FieldValue.arrayUnion(invitesList)});
break;
default:
}
通过将 updateData 更改为 setData 来修复它。
I looked around on stack and saw some ways to do it like copying the entire array and changing it where I need to, then adding it back.
这正是您应该如何修改 Firestore 文档中的数组内容。 Firestore 不支持按索引更新数组元素。
setData
如果文档不存在则创建一个新文档,但如果文档存在,数据将被覆盖。为防止这种情况发生,如果您希望附加数据,可以使用 SetOptions(merge: true)
:
set(someData, SetOptions(merge: true))
你也可以使用update
方法并向其提供更新后的数据,伪代码如下所示:
List<Map<String, dynamic>> updatedList = [...];
Map<String, dynamic> updatedData = {
'existing_map': updatedList,
};
var collection = FirebaseFirestore.instance.collection('collection');
collection
.doc('doc_id')
.update(updatedData);