【问题标题】:Having issues retrieving data from collection and subcollection of firestore从 firestore 的集合和子集合中检索数据时遇到问题
【发布时间】:2020-01-25 19:36:32
【问题描述】:

我正在尝试从 firestore 查询和检索一组文档和子文档,并将它们存储在我创建的一些自定义对象中。顶部集合映射到“Blockspaces”,子集合是“Blocks”。

我可以毫无问题地检索顶级集合,但是当我尝试遍历子集合文档并将它们存储在我的顶级对象中时,什么都不会返回。

这是执行工作的代码。 1.我从顶级集合中检索“blockspaces”数据。 2. 对于每个文档,然后我获取对子级集合(“块”)的引用,创建它并添加到顶级对象 3. 返回数据。

     Future<List<Blockspace>> fetchBlockSpaces(String ownerId)  async {

List<Blockspace> blockspacesObjects = List<Blockspace>();

var query = Firestore.instance
    .collection("blockspaces")
    .where('users', arrayContains: ownerId);

   await query.getDocuments().then((blockspaces) {

      blockspaces.documents.forEach((blockspace) async {

         Blockspace b1 = Blockspace(
          blockSpaceId: blockspace.documentID,
          ownerId: blockspace.data['ownerId'],
           icon: blockspace.data['icon'],
          color: blockspace.data['color'],
           name: blockspace.data['name'],
          );

          List<Block> currentBlocks = List<Block>();
          QuerySnapshot blocks = await blockspace.reference.collection('blocks').getDocuments();
          blocks.documents.forEach((block) {
            Block block1 = Block(
            name: block.data['name'],
            color: block.data['color'],
            icon: block.data['icon'],
            // userPermissions: userRoles,
            ownerId: block.data['ownerId']);
            currentBlocks.add(block1);
          });
          b1.blocks = currentBlocks;
          blockspacesObjects.add(b1);
      }); 
   });

    return blockspacesObjects;

}

这看起来应该很简单,我错过了什么?

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    如果您将awaitthen(和forEach)混合使用,则很难找到问题。要么完全使用await,要么使用then,最好不要混用(最好避免使用then)。请尝试使用简化代码:

    Future<List<Blockspace>> fetchBlockSpaces(String ownerId) async {
      final query = Firestore.instance
          .collection("blockspaces")
          .where('users', arrayContains: ownerId);
    
      final snapshot = await query.getDocuments();
      return Future.wait(snapshot.documents.map((bs) async {
        final blocksSnapshot =
            await bs.reference.collection('blocks').getDocuments();
        List<Block> currentBlocks = blocksSnapshot.documents.map((b) {
          return Block(
              name: b.data['name'],
              color: b.data['color'],
              icon: b.data['icon'],
              ownerId: b.data['ownerId']);
        }).toList();
        return Blockspace(
          blockSpaceId: bs.documentID,
          ownerId: bs.data['ownerId'],
          icon: bs.data['icon'],
          color: bs.data['color'],
          name: bs.data['name'],
          blocks: currentBlocks,
        );
      }));
    }
    

    【讨论】:

    • 我试着复制你的代码,编辑器给出了这个错误:返回类型 'List>' is not a 'Future>',由方法 'fetchBlockSpaces'.dart(return_of_invalid_type)
    猜你喜欢
    • 2021-07-03
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2020-09-01
    • 2018-07-07
    相关资源
    最近更新 更多