[英]reading data from firebase firestore collection at stream builder
我在使用 firebase fireStore 時遇到了麻煩。 有一個流生成器從項目集合中讀取數據。 在項目集合中有一些字段和另一個集合。 我對字段沒有任何問題,問題在於集合。 如何訪問 Stream Builder 中的那些集合?
StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
stream: CallApi().finalReference(reference: widget.finalReference),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(child: Text('snapshot Error:${snapshot.error}'));
}
if (snapshot.hasData) {
var snapData = snapshot.data!.docs;
if (kDebugMode) {
print(snapData.length);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: ListView.builder(
itemCount: snapData.length,
itemBuilder: (BuildContext context, int index) {
return ListItem(
mTitle: snapData[index].get('title') ?? '',
mSubTitle: snapData[index].get('address') ?? 'empty',
mPrice: snapData[index].get('price') ?? '',
mImageUrl: snapData[index].get('gallery')[0],
mOnTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailsPage(
adsTitle: snapData[index].get('title'),
adsSubTitle: snapData[index].get('subTitle'),
gallery: snapData[index].get('gallery'),
specFTitle: snapData[index].get('gallery'),
),
),
);
},
);
},
),
),
],
);
}
return const Center(child: CircularProgressIndicator());
},
),
這是火力基地
從 Firestore 讀取數據是一種淺層操作。 當您閱讀文檔時,不會自動閱讀其子集合。
所以如果你想從當前文檔的子集合中獲取數據,你將不得不為此啟動一個新的讀取操作。 如果您想在 UI 中顯示該數據,您可以為此使用新的嵌套StreamBuilder
或FutureBuilder
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.