dart - 如何在 Cloud Firestore 上的地图内添加地图?
问题描述
我想像这样在firestore上添加地图对象:
我能够得到这个,但只是为了products[0]
. 我确定我orders[0]
每次都想使用,但里面所有的东西都products
应该在那里。以下是我所做的:
await FirebaseFirestore.instance.collection('orders').add({
'dateTime': orders[0].dateTime,
'amount': orders[0].amount,
'products': {
orders[0].products[0].id: {
'title': orders[0].products[0].title,
'quantity': orders[0].products[0].quantity,
}
}
});
解决方案
您可以在那里使用reduce,例如:
await FirebaseFirestore.instance.collection('orders').add({
'dateTime': orders[0].dateTime,
'amount': orders[0].amount,
'products': {
orders[0].products.reduce((acc, {id, title, quantity }) => {
return { ...acc, { [id]: { title, quantity } } }
}, {})
}
});
推荐阅读
- python - 如何在 QTextEdit 中插入和清除占位符
- c++ - 取消来自辅助线程的请求以强制 MPI_Wait 返回
- google-bigquery - SQL UDF - 结构差异
- python - 优化大型数据集的迭代和替换
- kubernetes - Minikube 无法启动:“找不到节点‘minikube’”
- javascript - 解构嵌套参数,未定义错误
- html - 为什么占用这么多初始空间,如何控制呢?
- android - 使用 AS 同步时将运行 gradle 任务
- typescript - Observable 在 Lambda 中的处理方式是否与 Promise 不同?
- php - [Route: topic.index] [URI: topic/{id}/group] 缺少必需的参数