我試圖從每個檔案中獲取子集合 ex. clothes,notifer我有更多的檔案,這意味著我不知道它的id,我的專橫的邏輯是獲取主集合來獲取所有的檔案,然后為每個檔案獲取它的子集合,我用Future實作了,但我不能用Stream來回傳最后的子集合SnapShots ex. 通過Future它每次都會重建,如果我通過AutomaticKeepAliveClientMixin停止widget的重建,我不能得到任何Firesotre的變化。提前感謝。
這里是我的未來實作。
這里是我的Future實作,但我又需要這個實作的Stream ^_^:
Future<List<Category>> getPropertiesDocs() async {
List<QueryDocumentSnapshot> _firstListOfDocs = []。
List<Category> _categorListOfDocs = [];
List<QueryDocumentSnapshot> _secoudListOfDocs=[];
final QuerySnapshot result = await _firebaseFirestore.collection('categories').get() 。
result.docs.forEach((element) {
// print(element.id);.
_firstListOfDocs.add(element)。
});
for (var i in _firstListOfDocs) {
final QuerySnapshot snapshot2 = await i.reference.collection("properties").get()。
snapshot2.docs.forEach((element) {
_secoudListOfDocs.add(element);
_categorListOfDocs.add(Category.fromSnapShpt(element))。
});
}
_firstListOfDocs.clear()。
_secoudListOfDocs.clear()。
return _categorListOfDocs;
uj5u.com熱心網友回復:
從你未來的實作中,
- 你想獲得
categories集合中的所有檔案。 - 對于
categories集合中的每個檔案,你想獲得properties子集合。
對于第一個要求,我們可以簡單地流化類別集合。
對于第二個要求,我們不建議從每個categories子集合中流傳properties集合。這對于大型資料集來說是不太合適的。
作為替代,我們將流化集合組properties。流化集合組properties將獲取所有名稱為properties的集合(無論其位置如何)。為了有效地使用它,沒有其他的集合應該被命名為properties(除了你想要獲取的那些),或者你把你的集合重命名為一些獨特的東西,比如properties_categories。
//此streamBuilder將為categories集合獲取流。
StreamBuilder<QuerySnapshot>(
流。_firebaseFirestore.collection('categories').snapshots()。
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot<Delivery>> snapshot) {
if (snapshot.hasError) return Message()。
if (snapshot.connectionState == ConnectionState.waiting)
return Loading()。
print('類別快照結果')。
print(snapshot.data.docs.map((e) => e.data()).toList() )。
//_firstListOfDocs在下面給出(重命名為_categoryDocs)。
List<QueryDocumentSnapshot> _categoryDocs = snapshot.data.docs;
//這個streamBuilder將獲取所有名為properties的集合中的所有檔案。
return StreamBuilder<QuerySnapshot> (
流。_firebaseFirestore.collectionGroup('properties').snapshots()。
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> propertiesSnapshot) {
if (propertiesSnapshot.hasError) return Message()。
if (propertiesSnapshot.connectionState == ConnectionState.waiting)
return Loading()。
print('properties snapshot result')。
print(propertiesSnapshot.data.docs.map((e) => e.data()).toList() )。
//_secoudListOfDocs在下面給出(并重命名為_propertiesDocs)。
List<QueryDocumentSnapshot> _propertiesDocs = propertiesSnapshot.data.docs;
//_categorListOfDocs在下面給出(并重命名為_categories)。
List<Category> _categories = propertiesSnapshot.data.docs
.map((e) => Category.fromSnapShpt(e)).toList()。
//在這里回傳你的小工具。
return Text('Done') 。
},
);
},
)
如果你獲取categories集合資料的原因只是為了在它上面回圈并獲取properties集合,那么你可以洗掉上面的第一個streamBuilder,因為我們不需要用它來獲取properties集合。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/313112.html
標籤:

