作为项目的一部分,我们正在使用 Firestore,我们有一个配方集合,其中包含三个子集合:成分、方法和评论。
我可以将数据写入一个子集合,但我似乎无法弄清楚如何将第二个子集合写入同一文档。目前它生成两个文件。一种用于成分,另一种用于方法。
我们正在生成从 firestore 生成的唯一 ID,我知道我需要使用该文档 ID,但我不知道如何去做。在网上看,我只能看到如何写入一个子集合,而不是两个。
FirebaseFirestore.instance.collection('recipe').add({}).then((response) {
print(response.id);
FirebaseFirestore.instance
.collection("recipe")
.doc(response.id)
.collection("ingredients")
.add({
"allergies": allergies,
"category": categoryValue,
"description": descriptionController.text,
"image": "testing",
"ingredients": ingredients,
"prepTime": prepTime,
"protein": proteins,
"servings": servingsController.text,
"title": recipeNameController.text
}).then((response) {
print(response.id);
FirebaseFirestore.instance
.collection("recipe")
.doc(response.id)
.collection("method")
.add({
"method": methodSteps
});
});
});
答案 0 :(得分:0)
使用 async/await
应该更容易:
var recipeDocRef = await FirebaseFirestore.instance.collection('recipe').add({...});
await recipeDocRef.collection("ingredients").add({...});
await recipeDocRef.collection("method").add({...});
await recipeDocRef.collection("comments").add({...});
recipeDocRef
是一个 DocumentReference
,所以你只需要调用 collection
方法来声明这个文档的子集合(即 CollectionReference
s)。