Get list of documents inside different collections with Firestore in Flutter

烈酒焚心 提交于 2021-01-29 14:01:10

问题


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.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/59955680/get-list-of-documents-inside-different-collections-with-firestore-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!