gpt4 book ai didi

firebase - 使用 order by 子句时 Flutter Firestore 流不会更新

转载 作者:行者123 更新时间:2023-12-04 11:45:36 24 4
gpt4 key购买 nike

我正在使用以下命令从 firestore 返回一个流:

Stream<CompletedExerciseList> getExerciseStream(String uid, {int limit}) {

Stream<QuerySnapshot> snapshots;

try {
CollectionReference exerciseCollection = Firestore.instance.collection('users').document(uid).collection('completed_exercise');

if (limit != null) {
snapshots = exerciseCollection.orderBy('startTime', descending: false).orderBy('name', descending: true).limit(limit).snapshots();
} else {
snapshots = exerciseCollection.orderBy('startTime', descending: false).orderBy('name', descending: true).snapshots();
}
} catch (e) {
print(e);
}

return snapshots.map((list) => CompletedExerciseList(list.documents.map((doc) => ExerciseModel.fromMap(doc.data)).toList()));
}

上述方法在小部件构建时返回一个流,但是当集合中的文档更新时,流不会更新。但是,如果我删除 orderBy 子句,则在从集合中添加/删除文档时,流会正确更新。所以下面的代码有效,但是我需要能够订购和限制快照的结果。
Stream<CompletedExerciseList> getExerciseStream(String uid, {int limit}) {

Stream<QuerySnapshot> snapshots;

try {
CollectionReference exerciseCollection = Firestore.instance.collection('users').document(uid).collection('completed_exercise');

if (limit != null) {
snapshots = exerciseCollection.limit(limit).snapshots();
} else {
snapshots = exerciseCollection.snapshots();
}
} catch (e) {
print(e);
}

return snapshots.map((list) => CompletedExerciseList(list.documents.map((doc) => ExerciseModel.fromMap(doc.data)).toList()));
}

对于这两种情况,我的控制台都不会打印错误。我正在使用带有 Flutter SDK 的 Android Studio。

最佳答案

您应该能够检查 StreamBuilder 上的快照错误。至于对 Firestore 数据进行排序,您需要设置一个 index .

关于firebase - 使用 order by 子句时 Flutter Firestore 流不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59585818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com