firebase - 为什么 Firebase collection().get Documents() 返回 null 或 []?

标签 firebase flutter dart google-cloud-firestore

<分区>

我有一个名为 market 的集合,它有两个我可以在 firebase 中确认的文件。

enter image description here

我现在正在尝试获取该集合中的文档数量,如下所示:-

getAllItemsForSell()async {
    final snapshot = await Firestore.instance.collection('market').getDocuments();
    final documents = snapshot?.documents;

    print(documents); // []

  }

上面的函数打印出'[]',我做错了什么? 我在互联网上到处查看,但似乎没有任何帮助。

请帮忙?我是 Flutter Firebase 世界的新手,在此先感谢。

最佳答案

要查看该特定集合中的文档数量,请尝试从用户集合中获取 documentId 并通过将其传递给市场集合来对其进行迭代

 int sampleNum =0;
getAllItemsForSell()async {
        QuerySnapshot users =  await Firestore.instance.collection('market').getDocuments();
        for(DocumentSnapshot docs in users.documents){
          QuerySnapshot doc = await Firestore.instance.collection('market').document(docs.documentID).collection('marketPosts').getDocuments();
          sampleNum = doc.documents.length;
          //posts = doc.documents.map((doc) => Post.fromDocument(doc)).toList();
        }
        print(sampleNum.toString() +' **********');
      }

关于firebase - 为什么 Firebase collection().get Documents() 返回 null 或 []?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64627552/

相关文章:

Firebase 托管 : cache busting scripts on deploy

flutter - 如何在 flutter 中使用精度对整数进行舍入

flutter - 日期选择器未在TextFormField中设置输入

request - Dart - 使用 cookie 请求 GET

flutter - flutter 中对话框内的内容对齐?

firebase - Firebase 云存储规则能否针对 Firestore 数据进行验证?

Firebase 通知网络应用

android - 检查数据库值

pdf - 在 flutter 中将 PDF 在 Canvas 上解析和渲染为图像

Flutter json_serializable 构建失败