我正在嘗試獲取集合中的所有檔案并像這樣迭代它們:
class InitialRecipe extends StatelessWidget {
final UID;
CollectionReference collection =
FirebaseFirestore.instance.collection("recipes");
InitialRecipe(this.UID, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return FutureBuilder<QuerySnapshot>(
future: collection.get(), //.doc('VRWodus2pN2wXXHSz8JH').get(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
snapshot.data!.docs.forEach((e) {
print(e.data.toString());
print(e.data.runtimeType);
});
return Loading();
},
);
}
}
我拿不到檔案。相反,我收到一條訊息,告訴我 snapshot.data 的回傳值為空。

但是,我可以從集合中獲取一個特定的檔案,如下所示:
FirebaseFirestore.instance.collection("recipes").doc('VRWodus2pN2wXXHSz8JH').get()
難道我做錯了什么?
如何遍歷“食譜”集合中的檔案?
uj5u.com熱心網友回復:
看起來您AsyncSnapshot還沒有完成加載。
return FutureBuilder<QuerySnapshot>(
future: collection.get(), //.doc('VRWodus2pN2wXXHSz8JH').get(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.hasData) { // ??
snapshot.data!.docs.forEach((e) {
print(e.data.toString());
print(e.data.runtimeType);
});
}
return Loading();
},
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/398600.html
標籤:火力基地 扑 镖 谷歌云firestore
