从子集合消防仓库+FLUTTER检索信息



我是颤振和火球的新手。

我有一个名为";菜肴";在我有许多文件(如名称、描述等(的地方,我还有一个名为配料的子集合,其中我有id、名称和数量作为文件。

我想做的是取回所有有配料的菜。

我有型号为的餐具

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];
}
}

入口型号

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'

我如何解决它并检索我的菜信息?

谢谢大家

您需要获取dishes文档的id才能检索子集合。

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

这是一个Github回购

我认为getDishes()循环中有一些内容。这就是文档抛出数据的方式:

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

遵循文档:https://firebase.flutter.dev/docs/firestore/usage你能试着用forEach吗?

我发现了错误,它在getDishes方法中,正确的方法是:

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

感谢所有

最新更新