Juantums
Juantums

Reputation: 23

Retrieve information from sub collection firestore + FLUTTER

I'm new in flutter and firebase.

I have a collection called "dishes" where I have many documents (such as the name, description and so on), I have also a subcollection called ingredients where I have as a documents the id, name and quantity.

What I'm trying to do is retrieve all dishes with ingredients.

I have dishModel

class DishModel{
  static const ID = "id";
  static const DESCRIPTION = "description";
  static const IMAGE = "image";
  static const NAME = "name";
  static const INGREDIENTS = "ingredients";

  int _id;
  String _description;
  String _image;
  String _name;
  double _price;
  int _rates;
  double _rating;
  List<IngredientModel> _ingredients;

//GETTERS

  int get id => _id;

  String get description => _description;


  String get image => _image;



  String get name => _name;

  List<IngredientModel> get ingredients => _ingredients;

  DishModel.fromSnapshot(DocumentSnapshot snapshot){
    _id = snapshot.data()[ID];
    _description = snapshot.data()[DESCRIPTION];
    _image = snapshot.data()[IMAGE];
    _name = snapshot.data()[NAME];
  }
}

IngredientModel

class IngredientModel{
  static const ID = "id";
  static const NAME = "name";
  static const QUANTITY = "quantity";

  int _id;
  String _name;
  String _quantity;

  //GETTERS

  int get id => _id;

  String get name => _name;

  String get quantity => _quantity;


  IngredientModel.fromSnapshot(DocumentSnapshot snapshot){
    _id = snapshot.data()[ID];
    _name = snapshot.data()[NAME];
    _quantity = snapshot.data()[QUANTITY];

  }


}

DishHelper

class DishServices{
  String collection = "dishes";
  FirebaseFirestore _firestore = FirebaseFirestore.instance;
  String subcollection = "ingredients";

  Future<List<DishModel>> getDishes() async
  {
    DishModel dishTemp ;
    List<DishModel> dishes = [];
    _firestore.collection(collection).get().then((result) {
      for (DocumentSnapshot dish in result.docs) {
        dishTemp = DishModel.fromSnapshot(dish);
        dish.reference.collection(subcollection).get().then((ingredientRes) {
          for(DocumentSnapshot ingredient in ingredientRes.docs) {
            dishTemp.ingredients.add(IngredientModel.fromSnapshot(ingredient));
          }
        });
        dishes.add(dishTemp);
      }
      return dishes;
    });

  }

}

DishProvider

class DishProvider with ChangeNotifier{
  DishServices _dishServices = DishServices();
  List<DishModel> dishes = [];

  DishProvider.initialize(){
    _loadDishes();
  }

  _loadDishes() async{
    dishes = await _dishServices.getDishes();
    notifyListeners();
  }


}

in main I wrote

 runApp(MultiProvider(providers: [
    ChangeNotifierProvider.value(value: DishProvider.initialize())
  ],
      child: MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'example_app',
          theme: ThemeData(
            primarySwatch: Colors.red,
          ),
          home: ScreensController()
      )));
}

and in home (in build method)

final dishProvider =  Provider.of<DishProvider>(context);

but if I try to print print(dishProvider.dishes); I obtain flutter: null instead of flutter: Instance of 'DishModel'

how I can solve it and retrieve my dish information?

Thank you all

Upvotes: 0

Views: 195

Answers (3)

Juantums
Juantums

Reputation: 23

I found the error, it was in getDishes method, the right one is:

Future<List<DishModel>> getDishes() async
  {
    DishModel dishTemp ;
    List<DishModel> dishes = [];
    _firestore.collection(collection).get().then((result) {
      for (DocumentSnapshot dish in result.docs) {
        dishTemp = DishModel.fromSnapshot(dish);
        List<IngredientModel> ing_temp = [];
        dish.reference.collection(subcollection).get().then((ingredientRes) {
          for(DocumentSnapshot ingredient in ingredientRes.docs) {
            ing_temp.add(IngredientModel.fromSnapshot(ingredient));

          }
          dishTemp.ingredients = ing_temp;
        });
        dishes.add(dishTemp);
      }
      return dishes;
    });
    return dishes;
  }

thanks to all

Upvotes: 0

Zohaib Tariq
Zohaib Tariq

Reputation: 600

You need to get the id of the dishes document to retrieve the subcollection.

FirebaseFirestore.instance
              .collection('dishes')
              .doc(docId)
              .collection('ingredients')
              .snapshots(),

Here is a Github repo

Upvotes: 1

Marcel Dz
Marcel Dz

Reputation: 2714

I think there is something within the getDishes() loop. This is how the docs go threw the data:

FirebaseFirestore.instance
    .collection('users')
    .get()
    .then((QuerySnapshot querySnapshot) {
        querySnapshot.docs.forEach((doc) {
            print(doc["first_name"]);
        });
    });

Following the docs: https://firebase.flutter.dev/docs/firestore/usage Could you try to work with the forEach ?

Upvotes: 0

Related Questions