繁体   English   中英

Flutter firebase firestore combine 2查询

[英]Flutter firebase firestore combine 2 query

在下面的示例中,我想合并 2 个 firestore 查询,但我无法让它工作。

final List<Who> pagedData =
        await _query.get().then((QuerySnapshot snapshot) async {
      if (snapshot.docs.isNotEmpty) {
        _lastDocument = snapshot.docs.last;
      } else {
        _lastDocument = null;
      }
      return snapshot.docs.map((QueryDocumentSnapshot e) async {
        final data = e.data() as Map<String, dynamic>;
        Whoes w = Whoes.fromMap(e.data());
        User u = await _firestore
            .collection("user")
            .doc(data['s'])
            .get()
            .then((DocumentSnapshot documentSnapshot) => User.fromMap(
                documentSnapshot.data()));
        return Who(w, u);
      }).toList();
    });

当我在用户部分放置await时,事情变得混乱,我无法编辑它。

结果我想要 output 是List<Who>

我错过了什么?

它给了我这个错误:

返回类型'List<Future<Who>>'不是闭包上下文所要求的'Future<List<Who>>'

我解决了这个问题,我把它留给遇到这个问题的人

final List<Who> pagedData =
        await _query.get().then((QuerySnapshot snapshot) async {
      if (snapshot.docs.isNotEmpty) {
        _lastDocument = snapshot.docs.last;
      } else {
        _lastDocument = null;
      }
      Iterable<Future<Who>> futureWho = 
      snapshot.docs.map((QueryDocumentSnapshot e) async {
        final data = e.data() as Map<String, dynamic>;
        Whoes w = Whoes.fromMap(e.data());
        User u = await _firestore
            .collection("user")
            .doc(data['s'])
            .get()
            .then((DocumentSnapshot documentSnapshot) => User.fromMap(
                documentSnapshot.data()));
        return Who(w, u);
      });
      Future<List<Who>> listWho = Future.wait(futureWho);
      return listWho;
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM