简体   繁体   中英

Flutter display collection with subcollection from firestore

I am working on a Flutter application that displays a list of categories with items inside. the data is fetched from firebase.

Database structure:

类别

分类项目

I want to show data like this and it has to be real time:

  • Category 1
    • Item 1
    • Item 2
    • Item 3
  • Category 2
    • Item from category 2..

I tried to use Streambuilder, but this only fetches the categories. Could someone help me or point me in the right direction to also get the list of items as a snapshot for each category? Thanks in advance

A snapshot only contains the field and not the collection. So do I need another Streambuilder with the documentID of each category? This seems like a lot of work and heavy database reads, just to get the collection of an item already fetched.

StreamBuilder(
    stream: _db.collection('categories').document(widget.id).collection('categories').orderBy('tag', descending: true).snapshots(),
    builder: (context, snapshot) {
      if(!snapshot.hasData) {
        return Loader();
      } else {
        List<dynamic> docs = snapshot.data.documents;
        docs.forEach((element) {
          print(element['title']);
          print(element.collection('items')); // Does not exist
        });
        return Text('test');
      }
    },

Firestore can only load data from one collection (or collection group) at a time. Since you're trying to load data from the categories and items collections, you will need at least two separate read operations.

  StreamBuilder(
    stream: _db.collection('categories').document(widget.id).collection('categories').orderBy('tag', descending: true).snapshots(),
    builder: (context, snapshot) {
      if(!snapshot.hasData) {
        return Loader();
      } else {
        List<dynamic> docs = snapshot.data.documents;
        docs.forEach((element) {
          print(element['title']);
          element.reference.collection('items').get()...
        });
        return Text('test');
      }
    },

You'll need to wrap the get() call in a FutureBuilder , since it's another asynchronous load operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM