我有下面的 Future 回傳一個字串串列,而不是我想回傳具有相同檔案 ID 串列的流字串串列,我該怎么做?
Future<List<String>> getFollowingUidList(String uid) async{
final QuerySnapshot result = await FirebaseFirestore.instance.collection('following').doc(uid).collection('user_following').get();
final List<DocumentSnapshot> documents = result.docs;
List<String> followingList = [];
documents.forEach((snapshot) {
followingList.add(snapshot.id);
});
return followingList;
}
uj5u.com熱心網友回復:
按照FlutterFire檔案,該snapshots() 方法一的CollectionReference將回傳流。在 Dart 中,Streams 提供了一種map() 方法,可以讓您輕松地轉換 Stream 并回傳一個新的:
將此流的每個元素轉換為新的流事件。創建一個新流,該流使用 convert 函式將此流的每個元素轉換為新值,并發出結果。
基于此,可以從snapshots()來自 CollectionReference的Stream 中回傳一個帶有檔案 ID 的新 Stream 。CollectionReference Stream 是 type QuerySnapshot,根據檔案,它包含type的docs屬性。最后,這種型別可以為您提供從,包括檔案的 ID。 List<QueryDocumentSnapshot>dataDocumentSnapshots
Stream<List<String>> returnIDs() {
return Firestore.instance
.collection('testUsers')
.snapshots()
.map((querySnap) => querySnap.docs //Mapping Stream of CollectionReference to List<QueryDocumentSnapshot>
.map((doc) => doc.data.id) //Getting each document ID from the data property of QueryDocumentSnapshot
.toList());
}
我基于此示例撰寫了此代碼段,其中包含將 Streams 與 Firestore 結合使用的有用腳本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/346701.html
標籤:火力基地 扑 谷歌云firestore
