Mik*_*ink 4 dart flutter google-cloud-firestore rxdart
我有以下流,它接受一个组的流并返回其成员的流。我使用切换映射从组快照中获取成员。
但我有以下问题。我使用带有过滤器的 where 查询whereIn。但问题是,whereIn根据 firestore 文档,只能接受包含 10 个或更少条目的列表
限制 请注意 in 和 array-contains-any 的以下限制:
in 和 array-contains-any 支持最多 10 个比较值。
https://firebase.google.com/docs/firestore/query-data/queries#limitations
因此,在这种情况下,我在处理这个问题时遇到了一些困难。
Stream<List<UserModel>> groupMembersStream(Stream<GroupModel> groupStream) {
return groupStream.switchMap(
(value) => _fireStore
.collection(APIRoutes.users)
.where(FieldPath.documentId, whereIn: value.members.keys.toList(growable: false))
.snapshots()
.map((snapshot) =>
snapshot.documents.map((document) => UserModel.fromFirestore(document)).toList(growable: false)),
);
}
Run Code Online (Sandbox Code Playgroud)
因为我需要以组成员 ID 开头,所以我需要一个 switchMap。所以我不能简单地拆分组成员列表,然后对 10 个 id 的每个块进行单独的查询。
那么我该如何处理这个问题呢?
小智 5
Future<List<QueryDocumentSnapshot>> listItems(
List<dynamic> itemIds) async {
final chunks = partition(itemIds, 10);
final querySnapshots = await Future.wait(chunks.map((chunk) {
Query itemsQuery = FirebaseFirestore.instance
.collection('collection')
.where("id", whereIn: chunk);
return itemsQuery.get();
}).toList());
return querySnapshots == null
? []
: await Stream.fromIterable(querySnapshots)
.flatMap((qs) => Stream.fromIterable(qs.docs))
.toList();
}
Run Code Online (Sandbox Code Playgroud)
这种方式看起来更通用(您甚至可以使用供应商函数分解查询)。
| 归档时间: |
|
| 查看次数: |
4416 次 |
| 最近记录: |