Firebase Flatter Cloud Firestore处理子集合
目前,我正在创建一个简单的配方应用程序,它应该能够创建包含名称、说明和配料列表的配方 我在我的云firestore中创建了一个名为Firebase Flatter Cloud Firestore处理子集合,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,目前,我正在创建一个简单的配方应用程序,它应该能够创建包含名称、说明和配料列表的配方 我在我的云firestore中创建了一个名为recipes的集合,其中包含name,instruction字段。我还在recipes集合中创建了一个子集合,名为配料 在颤振应用程序中,我在StreamBuilder中获取这些数据,如下所示: [...] StreamBuilder<QuerySnapshot>( stream: _firestore.collection('recipes
recipes
的集合,其中包含name
,instruction
字段。我还在recipes集合中创建了一个子集合,名为配料
在颤振应用程序中,我在StreamBuilder
中获取这些数据,如下所示:
[...]
StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('recipes').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
// While loading/no items available show a CircularProgressIndicator
return Center(child: CircularProgressIndicator());
} else {
final recipesSnapshots = snapshot.data.documents;
List<RecipeCard> recipeCards = [];
for (var recipeSnapshot in recipesSnapshots) {
Recipe recipe = Recipe.fromSnapshot(recipeSnapshot);
// Retrieve all the ingredients from the subcollection and store them in the recipe object
recipeSnapshot.reference
.collection('ingredients')
.getDocuments()
.then((ingredientCollectionSnapshot) {
final ingredientSnapshots =
ingredientCollectionSnapshot.documents;
for (var ingredientSnapshot in ingredientSnapshots) {
recipe.ingredients
.add(Ingredient.fromSnapshot(ingredientSnapshot));
}
});
recipeCards.add(RecipeCard(recipe));
}
return Expanded(
child:
ListView(padding: EdgeInsets.all(12), children: recipeCards));
}
},
)
[...]
和我的配料
对象:
class Ingredient {
String documentID;
String name;
int quantity = 0;
String unit;
Ingredient(this.name, this.quantity, this.unit);
Ingredient.fromSnapshot(DocumentSnapshot snapshot) {
documentID = snapshot.documentID;
name = snapshot.data['name'];
quantity = snapshot.data['quantity'];
unit = snapshot.data['unit'];
}
Map<String, dynamic> toJson() {
return {
'name': name,
'quantity': quantity,
'unit': unit,
};
}
}
类成分{
字符串documentID;
字符串名;
整数数量=0;
弦单元;
成分(本名称、本数量、本单位);
Component.fromSnapshot(文档快照快照){
documentID=snapshot.documentID;
name=snapshot.data['name'];
数量=快照。数据[‘数量’];
单位=快照。数据['unit'];
}
映射到JSON(){
返回{
“名称”:名称,
“数量”:数量,
“单位”:单位,
};
}
}
RecipeCard
只是一个简单的Card
小部件,可以很好地列出配方信息。出于测试目的,您可以用一个简单的Text
小部件替换它
所以我关于子集合的问题是,如何有效地从每个配方中获取数据。正如您在myStreamBuilder
的builder函数中所看到的,我正在循环我的配方快照,并首先创建一个没有配料列表的Recipe
对象。随后,我用配方的documentID检索增量快照,并将它们添加到以前创建的配方的IngreditList中
我希望能找到一种方法,用名为construcor的fromSnapshot
创建配方
,并包含所有成分
我的第二个目标是如何将配料添加到菜谱子集合中?
我的应用程序中的工作流将是:
由于我的
Recipe
dart对象包含一个配料列表,因此我正在寻找一种理想的方法来保护/更新配方对象,并自动将所有配料保护到子集合。为什么不将配料存储在配方文档中?或者,在RecipeCard上,仅显示配方文档中的数据(除说明外的所有内容),然后当您单击配方卡时,它会进入一个新页面,显示成分(然后进行另一次读取)我这里的问题是我想在我的Recipe
对象中以列表的形式存储成分,因为一个配方可以包含0-n种成分。因此,基本上我正在寻找一种方便的方法来存储与父对象相关的子对象列表。
class Ingredient {
String documentID;
String name;
int quantity = 0;
String unit;
Ingredient(this.name, this.quantity, this.unit);
Ingredient.fromSnapshot(DocumentSnapshot snapshot) {
documentID = snapshot.documentID;
name = snapshot.data['name'];
quantity = snapshot.data['quantity'];
unit = snapshot.data['unit'];
}
Map<String, dynamic> toJson() {
return {
'name': name,
'quantity': quantity,
'unit': unit,
};
}
}