在Flutter中使用Firestore获取不同集合中的文档列表 [英] Get list of documents inside different collections with Firestore in Flutter

查看:52
本文介绍了在Flutter中使用Firestore获取不同集合中的文档列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有不同产品组合的用户列表.每个投资组合具有相同的属性,但也具有投资的集合.就像这样:

I have a list of users with different portfolios. Each portfolio has the same properties but also a collection of investments. It'd be something like this:

Map user = {
  "name": "John",
  "portfolios": [ // collection
    {
      "title": "My portfolio 1",
      "investments": [ // collection
        {"title": "My investment 1.2"},
        {"title": "My investment 1.2"}
      ]
    },
    {
      "title": "My portfolio 2",
      "investments": [ // collection
        {"title": "My investment 2.1"},
        {"title": "My investment 2.2"},
        {"title": "My investment 2.3"}
      ]
    }
  ]
};

要获取我的投资组合,我需要执行以下操作:

To get my portfolios I do something like this:

Future getMyPortfolios() async {
    final userId = await authService.getUserId();
    final portfolios = await Firestore.instance
        .collection('users')
        .document(userId)
        .collection('portfolios')
        .getDocuments();
    return portfolios.documents;
  }

但这只会返回带有标题的投资组合,而不是带有投资的内部集合.

But this only returns the portfolios with their titles, not the inner collection with the investments.

我可以使用这些ID再次拨打电话,但我想知道是否有一种更快的方法来从该特定用户那里获取所有信息.

I could make another call with these IDs, but I was wondering if there is a quicker way and get everything from that particular user.

推荐答案

没有更快的选择.您将需要一个单独的查询来获取其他任何集合中的文档,即使它们是子集合.Firestore查询始终是浅"的.没有深层"查询会在子集合中获取嵌套文档.

There is no faster option. You will need a separate query to get the documents in any other collection, even if they are subcollections. Firestore queries are always "shallow". There are no "deep" queries that get nested documents in subcollections.

这篇关于在Flutter中使用Firestore获取不同集合中的文档列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆