【问题标题】:Unable to Query a Subcollection From StreamBuilder (Flutter)无法从 StreamBuilder (Flutter) 查询子集合
【发布时间】:2020-08-11 14:54:30
【问题描述】:

我正在尝试从 StreamBuilder 查询子集合,但查询返回零个文档,尽管子集合中有一个文档。查询父集合返回正确数量的文档,并且已验证传递给子集合的文档 ID。我错过了什么?

StreamBuilder<QuerySnapshot>(
                stream: _firestore
                    .collection('books')
                    .document(documentID)
                    .collection('enquiries')
                    .snapshots(),
                builder: (context, snapshot) {
                  if (!snapshot.hasData) {
                    return Text('You have not received any enquiries yet!');
                  } else {
                    final numberOfDocs = snapshot.data.documents.length;
                    print(numberOfDocs); // HELP HERE! returns 0, although a document exists in the subcollection
                    print(documentID);   // returns the expected document ID
                    ...

Firestore Subcollection:

documentID passed to the .document() method

【问题讨论】:

  • 添加你的firestore
  • @PeterHaddad - final _firestore = Firestore.instance;抱歉,StackOverflow 的新用户进行了多次编辑
  • 我们可以查看您的 Firestore 仪表板的屏幕截图以及 documentID 的值吗?
  • @dshukertjr:我已经添加了图片,但它们显示为链接,因为我是 Stack Overflow 上的新用户
  • @yesharish 谢谢。这很有帮助!

标签: firebase flutter dart google-cloud-firestore stream-builder


【解决方案1】:

这对于 Firestore 的初学者来说非常混乱,但从您提供的屏幕截图来看,您似乎实际上在子集合中没有任何文档。

当文档ID名称为斜体时,表示该文档有子集合,但没有文档本身。

当您创建文档的子集合然后删除该文档时会发生这种情况。

确保在书籍/kOhVCohNlbQWkGem06RF/enquiries 中创建一些文档,您应该一切顺利!

【讨论】:

  • 谢谢!这正是我所做的......创建一个没有字段的文档只是为了添加一个子集合
猜你喜欢
  • 2020-12-02
  • 1970-01-01
  • 2021-04-08
  • 2020-10-10
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
相关资源
最近更新 更多