如何解决从子集合 firestore + FLUTTER 中检索信息
我是 Flutter 和 firebase 的新手。
我有一个名为“dishes”的集合,其中包含许多文档(例如名称、描述等),还有一个名为成分的子集合,其中我将 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
而不是 Flutter: Instance of 'dishModel'
如何解决并检索我的菜品信息?
谢谢大家
解决方法
您需要获取菜肴文档的 id 才能检索子集合。
FirebaseFirestore.instance
.collection('dishes')
.doc(docId)
.collection('ingredients')
.snapshots(),
Here 是一个 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;
}
感谢大家
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。