我正在试验 Firestore 中的数组和 map /对象。我想知道如何从数组中删除特定的 map 。 我试过这样的事情:
await Firestore.instance.collection('users').document(interestedInID).get().then((val){
return val.data['usersInterested'].removeWhere((item)=>
item['userID'] == userID
);
}).catchError((e){
print(e);
});
但我在终端中收到此错误:
Unsupported operation: Cannot remove from a fixed-length list
我不太明白这是什么意思。我做了一些谷歌搜索,固定长度的列表正是它所说的。这是一个固定长度的列表,不能更改,但必须明确声明固定长度的列表。另一方面,不需要声明可增长列表。 我没有在我的 firestore 中声明固定长度的列表,但它一直说我不能从中删除元素。但是我可以添加/推送元素并使用以下方法删除它们:
'key': FieldValue.arrayRemove([value])
但我不知道如何根据特定条件删除元素。在本例中为用户 ID。
有什么建议吗? 非常感谢!
最佳答案
想通了。
await Firestore.instance.collection('users').document(interestedInID).updateData({
'usersInterested': FieldValue.arrayRemove([{}.remove(userID)])
});
我不确定,但我认为 get() 只允许您阅读文档,但不允许对其进行任何更改。 不管怎样,现在可以了
关于firebase - 从 firebase 中的数组中删除特定的 map /对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54393063/