Получить информацию из подгруппы firestore + FLUTTER

Я новичок в flutter и firebase.

У меня есть коллекция, называемая блюдами, где у меня есть много документов (таких как имя, описание и т. Д.), У меня также есть подколлекция под названием ингредиенты, где у меня есть в качестве документов идентификатор, имя и количество.

Я пытаюсь получить все блюда с ингредиентами.

У меня есть блюдо

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(); } }

в основном я написал

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

и дома (в методе сборки)

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

но если я попытаюсь напечатать print(dishProvider.dishes);, я получу flutter: null вместо флаттера: Instance of ‘DishModel’

как я могу решить эту проблему и получить информацию о моем блюде?

Спасибо вам всем

Источник: ledsshop.ru

Стиль жизни - Здоровье!