我正在使用 flutter 和 firebase 做一些工作,所以我无法弄清楚如何将新元素添加到现有文档中,该文档具有带有一些已创建字段的现有 map ,因此……例如 map 是这样的
'Users':{uid0:{'Name':{'userName0'},uid1:{'Name':{'userName1'}}
这是我的实际代码。
Firestore.instance.collection('on_going_matches').where('userSlot',isGreaterThan: 0).snapshots().listen(
(data) => {
Firestore.instance.runTransaction((transaction) async {
await transaction.update(Firestore.instance.collection("on_going_matches").document(data.documents[0].documentID), {
'users': {uid:{'Name':name}},
'userSlot': data.documents[0]['userSlot'] - 1
}
);
})
});
我也有另一个问题,当“where”条件时我能做什么
"where('userSlot',isGreaterThan: 0)"
未找到,我如何像 if/else 语句一样使用它,因此,如果未找到 Firestore 查询,请执行操作。
最佳答案
更新现有文档:myDocument.setData(myMap, merge: true);
关于flutter - 如何将元素添加到现有 map Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56929127/