首页 > 解决方案 > 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) => {

  })
});

标签: firebasefluttergoogle-cloud-firestore

解决方案


 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(),
        );
      },
    );

推荐阅读