Using Flutter Firestore plug in, do something for

2020-05-07 10:11发布

问题:

To help me better understand Flutter and Firebase, I'm making a list sharing app. I'm working on the list home screen that will show a reorderable list tile view with a tile for each of the users' lists, I have not began to work on whats inside of these lists. I have firestore set up so that each list is a sub collection, now I want to create a tile for each sub collection in that user's document ( a list of that user's lists). I'm having a tough time telling flutter to do something for each sub collection without using a specific sub collection's name. I wanted to pass in a list title to each tile by making a collection reference for each sub collection( list) and calling .id on each one. Using the collection ID as the title, I'm not yet sure if I can do this or if ill have to make the list title a field inside of each list. Either way, I need to find out how to do something for each subcollection inside a particular document. .forEach seems to only work on document fields, not subcollections? What am I doing wrong? I'm sure there is a better way to go about this. I have not included any code as this is a big picture kind of question.

回答1:

There is no method in the Firestore client-side SDKs to get all collections (or subcollections under a specific document). Such API does exist in the server-side SDKs, but not in the client-side SDKs.

See:

  • Fetching all collections in Firestore
  • How to get all of the collection ids from document on Firestore?

So you'll need to know the collections already, typically by changing your data model. For example by creating a document for each list's metadata, and then storing the list items in a subcollection with a known name under that document. That way you can get all lists by querying for the documents, which is possible within the API.