【发布时间】:2020-12-28 20:36:19
【问题描述】:
我在 Firestore 中有一个名为 Categories 的集合。每个类别都有一个手动添加的索引。当我使用 GridView 遍历类别以在 UI 上显示它们时,类别的顺序是按照它们最初在 Firestore 中创建的方式来选择的。
这里是我的代码:
database.dart
Future<List<CategoriesModel>> getCategoriesList({String partnerStoreId}) async {
List<CategoriesModel> categories = List();
List<ProductsModel> products = List();
QuerySnapshot snapshot;
// Products
snapshot = await productsCollection
.where('partnerInfo.storeId', isEqualTo: '$partnerStoreId')
.getDocuments();
products = snapshot.documents.map((doc) => ProductsModel.fromSnapshot(doc)).toList();
// Categories
snapshot = await categoriesCollection.getDocuments();
categories =
snapshot.documents.map((doc) => CategoriesModel.fromSnapshot(doc)).toList();
int ind = 0;
categories.forEach((category) {
products.forEach((product) {
if(product.category == category.enId) {
category.productCount = category.productCount + 1;
}
});
categories[ind] = category;
ind++;
});
return categories;
}
在 Firestore 中,每个类别都有一个名为 ux 的键和一个名为 index 的子键,其中手动添加了一个数字。
问题:当我在 UI 中显示类别时,我需要先显示 ux.index = 11,然后是 ux.index = 3,然后是其他所有内容。
我正在尝试更改这一行:
snapshot = await categoriesCollection.getDocuments();
我正在尝试使用 .where、startAt、orderBy 等....它将查询 Firestore。
以下是我尝试过的一些示例:
// Here only number 11 is displayed
snapshot = await categoriesCollection.where('ux.index', isEqualTo: 11).getDocuments();
// Here nothing is displayed
snapshot = await categoriesCollection.where('ux.index', isEqualTo: 11).where('ux.index', isEqualTo: 3).getDocuments();
// Nothing is displayed, and even if I use only isLessThanOrEqualTo, the number 11 item is still NOT the first
snapshot = await categoriesCollection.where('ux.index', isLessThanOrEqualTo: 11).where('ux.index', isGreaterThan: 11).getDocuments();
有人可以帮忙吗?目前这让我发疯。
提前感谢
乔
【问题讨论】:
-
能否请您发布您的 Firestore 数据树结构是什么样的?包括 categoryCollection,以及您存储 ux.subkeys 的方式
标签: flutter dart google-cloud-firestore