无法从 StreamBuilder (Flutter) 查询子集合
Unable to Query a Subcollection From StreamBuilder (Flutter)
我正在尝试从 StreamBuilder 查询子集合,但查询 returns 零个文档,尽管子集合中有一个文档。查询父集合 returns 正确的文档数和传递给子集合的文档 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 的初学者来说非常混乱,但是从您提供的屏幕截图来看,您似乎实际上没有子集合中的任何文档。
当文档ID的名称为斜体时,表示该文档有子集,但没有文档本身。
当您创建文档的子集然后删除该文档时,就会发生这种情况。
确保在 books/kOhVCohNlbQWkGem06RF/enquiries 中创建一些文档,您应该可以开始了!
我正在尝试从 StreamBuilder 查询子集合,但查询 returns 零个文档,尽管子集合中有一个文档。查询父集合 returns 正确的文档数和传递给子集合的文档 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 的初学者来说非常混乱,但是从您提供的屏幕截图来看,您似乎实际上没有子集合中的任何文档。
当文档ID的名称为斜体时,表示该文档有子集,但没有文档本身。
当您创建文档的子集然后删除该文档时,就会发生这种情况。
确保在 books/kOhVCohNlbQWkGem06RF/enquiries 中创建一些文档,您应该可以开始了!