0
votes

I'm recently started using Firebase firestore. I'm stuck at the point were the collection have sub collections and get those items to my model.

enter image description here

Let say I have this collection

meats
  \ id : 1
  \ name : Chicken
 subMeats 
      \ id : 11
      \ name : Country Chicken
      \ id : 12
      \ name : Broiler Chicken
meats 
  \ id : 2
  \ name : Pork
subMeats
      \ id : 21  
      \ name : White Pork
      \ id : 22
      \ name : Black Pork

     return meatTypeCollection.get().then((value) {
  value.docs.forEach((mainDoc) {
   // MainMeatEntity.fromSnapshot(mainDoc)    
    _logger.i(mainDoc.data()['name']);
    meatTypeCollection
        .doc(mainDoc.id)
        .collection('subMeats')
        .get()
        .then((snapShots) {
      snapShots.docs.forEach((doc) {
        _logger.i(doc.data()['name']);
        MainMeat.fromEntity(MainMeatEntity.fromSnapshot(doc));
      });
    });
  });
  //return MeatTypes.fromEntity(MeatTypeEntity.fromJson(value.docs));
}).catchError((error) => _logger.e("Failed to load meat types : $error"));

The above does not capture the collection. I use entity to model.

   import 'package:flutter/cupertino.dart';
   import 'entities/main_meat_entity.dart';
   import 'entities/meat_type_entity.dart';

    @immutable
    class MainMeat {
    final String id;
    final String name;
    final MeatTypeEntity subMeats;
    final String shopId;

 const MainMeat({
   this.id,
   this.name,
   this.subMeats,
   this.shopId,
 });

 static MainMeat fromEntity(MainMeatEntity mainMeatEntity) {
   return MainMeat(
    id: mainMeatEntity.id,
    name: mainMeatEntity.name,
    subMeats: mainMeatEntity.subMeats,
    shopId: mainMeatEntity.shopId,
   );
  }

  MainMeatEntity toEntity() {
     return MainMeatEntity(id, name, subMeats, shopId);
  }

  static const empty = MainMeat(id: '', shopId: "", name: "");
   }
  -----------------------------****************--------------------
  part 'meat_type_entity.g.dart';

 @JsonSerializable()
 class MeatTypeEntity extends Equatable {
  final String id;
  final String name;
  final String shopId;

  const MeatTypeEntity(this.id, this.name, this.shopId);

  factory MeatTypeEntity.fromJson(Map<String, dynamic> json) =>
  _$MeatTypeEntityFromJson(json);

   Map<String, dynamic> toJson() => _$MeatTypeEntityToJson(this);

   @override
   List<Object> get props => [
     id,
     name,
     shopId,
    ];

   static MeatTypeEntity fromSnapshot(DocumentSnapshot snap) {
    return MeatTypeEntity(
    snap.id,
    snap.data()['name'],
    snap.data()["shopId"],
      );
     }

    Map<String, Object> toDocument() {
     return {
     "id": id,
     'mainMeat': name,
     "shopId": shopId,
     };
    }
   }

I can list it's collection and sub collections fine. But not sure to load into my model. Any help appreciated thanks.

2
Hi! I don't quite understand what your struggle is here. Do you mean you don't know how you can convert firestore snapshot to Dart class? - dshukertjr
yes, difficult to structure in my model - EngineSense
you have to use different models for both. - Faiizii Awan
Have you attempted to write any code? If so, could you share them? - dshukertjr
Yup, I did but hard to get it done. - EngineSense

2 Answers

0
votes

You can't store subCollection to collection directly, Read FireStore data model section.

So your structure will be look like Meal(document) -> all meal collection -> Sub Meal (document) -> all sub meals collection.

If you want to read the data like firebase database, you can't read the complete tree/hierarchy at a time.

0
votes

So assuming this is a Dart question of how to create models, I would create something like this:

class Meat {
  final String id;
  final String name;
  final List<SubMeat> subMeats;

  Meat({
    @required this.id,
    @required this.name,
    this.subMeats,
  });
}

class SubMeat {
  final String id;
  final String name;

  SubMeat({
    @required this.id,
    @required this.name,
  });
}