Dani
Dani

Reputation: 4177

Get list of documents inside different collections with Firestore in Flutter

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.

Upvotes: 0

Views: 451

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317352

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.

Upvotes: 1

Related Questions