【问题标题】:Firebase: Iterate over mapFirebase:迭代地图
【发布时间】:2021-05-16 09:55:13
【问题描述】:

我有一个名为“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

这是我最近做的 attampt,但它说“DocumentSnapshot”类型没有方法“forEach”,尽管我认为我几乎在每个教程中都说这个 attampt。但是我的不行!

    var products = await db.collection("inventar").doc("vqQXArtqnFyAlkPC1PHr").get().then((snapshot) => {
  snapshot.forEach((doc) => {

  })
});

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:
     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(),
            );
          },
        );
    

    【讨论】:

    • 感谢您的回复!如果我使用您的代码,我会收到一条错误消息“没有为类型 'AsyncSnapshot' 定义 getter 'docs'。尝试导入定义 'docs' 的库,将名称更正为现有名称getter,或定义一个名为 'docs' 的 getter 或字段。除此之外,我认为,您的代码会为地图中的每个条目生成一个新表,不幸的是这不是我想要的。
    • 这个 api FirebaseFirestore.instance.collection("inventar").where("verfallsdatum").snapshots() 将从集合中返回多个文档,它是 QuerySnapshot 的实例,并且它应该有 .docs getter,如果不是这种情况,它将返回 List&lt;QueryDocumentSnapshot&gt;你,你能发布一下你使用的 cloud_firestore 的版本是cloud_firestore: ^0.16.0
    • 哦,我的错应该是snapshot.data.docs.map而不是snapshot.docs.map我刚刚更正了
    • 嗨,阿拉,我刚刚尝试了您的方法,但遇到了几个错误。首先,ListView-Widget 的 children 属性显示“不能将参数类型 'List' 分配给参数类型 'List'”和“不能无条件访问属性 'docs'因为接收者可以是'null'。”其次,返回表的行显示“返回类型 'Table' 不是 'MapEntry<_ _>',正如闭包上下文所要求的那样”。不要认为我们在这里走的是正确的道路。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 2020-08-11
    • 2013-03-16
    • 1970-01-01
    • 2016-06-28
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多