【发布时间】:2020-12-23 00:20:07
【问题描述】:
我正在使用以下颤振代码来查询使用字段时间戳对数据进行排序的 Firestore。
var results = Firestore.instance.collection('post').orderBy('timestamp').getDocuments().then((value) {
var list = value.documents;
return list.map((doc) {
return doc.documentID;
}).toList();
});
当我运行此代码时,它会抛出以下异常,说明需要索引:
W/Firestore(31110): (21.3.0) [Firestore]: Listen for Query(app/jQH7Fp9xCZWYiqZRe7lE/post where readAccess array_contains_any [WzKImODx6WYVqdSW3D9Az3xrUnM2, PUBLIC] order by -timestamp, -name ) 失败:Status{code=FAILED_PRECONDITION, description=查询需要索引。你可以在这里创建它:https://console.firebase.google.com/v1/r/project/...。
这个例外甚至带有一个很好的链接。打开该链接时,会弹出一个漂亮的 UI,让我能够创建索引,只需单击一下:
问题:虽然上面看起来很简单,但我对此并不满意。我更喜欢能够从fluttercode创建索引。在代码中,我正在寻找类似下面的内容:
Firestore.instance.collection('post').API-TO-CREATE-INDEX('timestamp');
它存在吗?请指教。非常感谢。
【问题讨论】:
标签: flutter google-cloud-firestore