firebase - Firebase:迭代地图
问题描述
我有一个名为“inventar”的集合,其中包含一个带有自动生成值的文档,其中包含一个我想要迭代的地图。
请注意,键会有所不同,因为用户将指定它。
如何迭代此映射,以便可以在下面列出的表格单元格中输出键和值?
new StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection("inventar")
.where("verfallsdatum")
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return new Table(
children: [
new TableRow(children: [
new TableCell(child: new Text("Produkt")),
new TableCell(child: new Text("Verfallsdatum")),
]),
// how to iterate here?
new TableRow(
children: [
new TableCell(
child: new Text("key"),
),
new TableCell(
child: new Text("value"),
),
]
)
]);
},
)
编辑:我试图从我的数据库中取出这些数据,因为差不多一个月了!我有哪些重大错误或误解,我无法查询包含地图的单个文档并将其输出为表格?这项任务执行起来是否如此艰巨,还是我只是愚蠢?:D
这是我最近做的尝试,但它说“DocumentSnapshot”类型没有方法“forEach”,尽管我认为我几乎在每个教程中都说这个attampt。但我的不是工作!
var products = await db.collection("inventar").doc("vqQXArtqnFyAlkPC1PHr").get().then((snapshot) => {
snapshot.forEach((doc) => {
})
});
解决方案
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection("inventar")
.where("verfallsdatum")
.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return ListView(
children: snapshot.data.docs.map((documentSnapshot) {
var _data = documentSnapshot.data();
return _data.map((key, value) {
return new Table(children: [
new TableRow(children: [
new TableCell(child: new Text("Produkt")),
new TableCell(child: new Text("Verfallsdatum")),
]),
// how to iterate here?
new TableRow(children: [
new TableCell(
child: new Text(key),
),
new TableCell(
child: new Text(value),
),
])
]);
}).toList();
}).toList(),
);
},
);
推荐阅读
- java - 如何在 Spring Boot 中更改 OAuth2 的过滤级别?
- asp.net-core - 如何在 Asp.Net Core 2 中正确注册自定义 IdentityServer ConfigurationDbContext?
- java - 内省 (JavaFx)
- javascript - React:webpack 4——一个块没有找到另一个
- python - 用scrapy抓取网站上的所有评论
- r - Docker 中的 shinyDirButton 与 fileInput
- java - IntelliJ - 创建新项目时缺少 Java 主类
- python - 带有 datetime64 [ns, UTC] 字段的 Pandas Timedelta 错误
- .htaccess - 试图将本地 url 重写为另一个
- javascript - 从 JS 对象数组中获取数据