我使用 flutter 和 firebase firestore 資料庫創建了一個簡單的應用程式。有“用戶”集合,每個用戶都有“帖子”集合。每個帖子可能有一個或多個由不同用戶添加的帖子。無論用戶如何,我都試圖獲取所有帖子。但是,我當前的功能是為閱讀記錄而撰寫的,僅顯示與登錄用戶相關的帖子,而不是所有帖子。
Stream<QuerySnapshot> readItems({required String collectionName}) {
CollectionReference detailCollection = _firestore
.collection(mainCollectionName!)
.doc(userUid)
.collection(collectionName);
return detailCollection.snapshots();
}
在這里,我分別將 'users'、'login user's uid' 和 'posts' 作為 mainCollectionName、userUid 和 collectionName 傳遞。任何人都可以指導我如何獲取所有帖子而不管用戶如何?

uj5u.com熱心網友回復:
可以從集合中的所有檔案中獲取所有子集合資料。試試這個方法
_firestore
.collection(`${mainCollectionName}/*/${collectionName}`)
.get()
有關查詢集合和子集合的更多詳細資訊,請參閱此頁面 - https://firebase.googleblog.com/2019/06/understanding-collection-group-queries.html
uj5u.com熱心網友回復:
搜索后,我在這里找到了解決方案。以下方法提供了所需的輸出。
Stream<QuerySnapshot> readItems({required String collectionName}) {
Query<Map<String, dynamic>> detailCollection = _firestore.collectionGroup(collectionName);
return detailCollection.snapshots();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/362041.html
標籤:火力基地 扑 谷歌云firestore 收藏
