Firebase 在查询中使用orderBy和startAfterDocument时出错
在同一查询中使用orderBy和startAfterDocument时,出现以下错误: 失败:状态{code=failed\u前提条件,description=查询需要索引。您可以在此处创建它: 当我尝试接收下一组文档时,发现此错误。 尝试使用错误中提供的链接创建索引,但该索引是使用“finalTimeStamp”字段生成的单个索引,firestore抛出以下异常Firebase 在查询中使用orderBy和startAfterDocument时出错,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,在同一查询中使用orderBy和startAfterDocument时,出现以下错误: 失败:状态{code=failed\u前提条件,description=查询需要索引。您可以在此处创建它: 当我尝试接收下一组文档时,发现此错误。 尝试使用错误中提供的链接创建索引,但该索引是使用“finalTimeStamp”字段生成的单个索引,firestore抛出以下异常 this index is not necessary, configure using single field index co
this index is not necessary, configure using single field index controls
在我的单字段索引中,已启用集合范围的降序索引
我的代码:
if(_lastVisible==null){
试一试{
数据=等待聊天列表
.document(widget.currentUserId)
.collection(“收件箱”)
.orderBy('finalTimeStamp',降序:true)
.限额(10)
.getDocuments();
}捕获(e){
打印(“捕获错误1”);
}
}否则{
试一试{
数据=等待聊天列表
.document(widget.currentUserId)
.collection(“收件箱”)
.orderBy('finalTimeStamp',降序:true)
.startAfterDocument(\u lastVisible)
.限额(10)
.getDocuments();
}捕获(e){
打印(“捕获错误2”);
}
}
应在同一字段上执行orderBy和startAfter或startAfter文档
在我上面的问题中,我在finalTimeStamp字段上执行了orderBy,并使用startAfterDocument,它在对文档进行排序时会考虑文档ID。因此,firestore会抛出一个错误
您可以通过两种方式解决此问题:
您在哪里设置
\u lastVisible
?我在这个if-else条件之后设置了它。如果data.documents.length>0,我将最后一个文档设置为\u lastVisible。我遇到了同样的问题。我缩小了范围,似乎只有当分页查询在子集合上按降序时才会失败。您尝试过ascendin吗g:真的
?按任何字段升序对我来说都非常有效。无论如何,我已经向他们的支持团队开了一张罚单,因为它可能是SDK上的一个错误。我很抱歉没有提前发布此消息,但这是firebase查询中的一个限制。startAfter只能在您设置了orderBy查询的索引上执行。@rajeshc那么使用startAfter(_lastVisible.field)而不是startAfterDocument()可以解决这个问题吗?