【发布时间】:2021-05-26 19:17:40
【问题描述】:
我在将对象数组(表)添加到对象数组(餐厅)时遇到了麻烦。我的意思是,我有以下 Firestore 结构:
... (other data)
restaurants: [
0: {
name: name_0
...
tables: [
0: { no_chairs: 5, ... },
]
},
...
]
我知道如何向餐馆添加新对象,但如果表列表包含任何数据,我在添加新餐馆时遇到问题。
我的代码:
var collection = firestore.collection('usersData')
.doc(this.currentUser.uid).collection('buisness').doc('restaurants');
collection.update({
'restaurants': FieldValue.arrayUnion([restaurant.toJson()]),
}).then( ... // reaction based on api answer
class Restaurant:
Map<String, dynamic> toJson() => {
'name': name,
...
'tables': tables!=null? [tables] : [], // I think that I should modify this line
};
class Table:
Map<String, dynamic> toJson()=> { // Or this method / use another
'no_chairs': no_chairs,
...
};
有什么简单的方法吗?最好不要修改firestore文件结构,但如果有必要我愿意这样做。
补充问题: 如何修改表格列表中的对象?比如我想把 no_chairs 改成 7
【问题讨论】: