firebase - 如何在 Firebase 中索引查询?
问题描述
我正在尝试使用以下方式获取包含与用户位置匹配的位置字段的文档:
getLocationListings(placemark) async {
setState(() {
isLoading = true;
});
QuerySnapshot snapshot = await FirebaseFirestore.instance
.collectionGroup("posts")
.where('location', isGreaterThanOrEqualTo: placemark.locality)
.get();
setState(() {
posts = snapshot.docs.map((doc) => Post.fromDocument(doc)).toList();
isLoading = false;
});
}
我收到错误:
Operation was rejected because the system is not in a state required for the operation's execution. If performing a query, ensure it has been indexed via the Firebase console.
我该如何纠正?
解决方案
您为该集合组创建的索引可能有问题。我建议你删除你创建的索引并再次运行该函数,它会抛出一个错误,如果你要通过执行来检查 android 设备日志
adb logcat
在终端上,您将看到如下消息:
'FAILED_PRECONDITION:查询需要索引。您可以在此处创建它:https://console.firebase.google.com/v1/r/project/XXXXXXXXX/firestore/indexes?create_composite=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'。
只需单击链接并创建建议的索引,这应该可以解决问题。