Flutter firebase firestore组合2查询

发布于 2025-01-11 12:25:46 字数 1038 浏览 0 评论 0 原文

在下面的示例中,我想组合 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 放在用户部分时,事情变得混乱并且我无法编辑它。

我想要输出的结果是 List

我缺少什么?

它给了我这个错误:

返回类型 'List>' 不是 'Future>',根据闭包的要求上下文。

In the example below, I want to combine 2 firestore queries, but I could not get it to work.

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();
    });

When I put await in the user part, things get confused and I couldn't edit it.

What I want to output as a result is List<Who>

What am I missing?

It gives me this error:

The return type 'List<Future<Who>>' isn't a 'Future<List<Who>>', as required by the closure's context.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁与争疯 2025-01-18 12:25:46

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

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;
    });

I solved the problem, I leave it here for anyone who encounters this

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;
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文