【问题标题】:Flutter Firestore queries. I need to start from a specific index to show a order listFlutter Firestore 查询。我需要从特定索引开始显示订单列表
【发布时间】: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


【解决方案1】:

让我解释一下为什么你的最后两个查询什么都不返回。

使用多个where 表示and,它允许您一次传递多个查询。从字面上看就像query1 and query2 and ...

请参阅下面的说明。

// Here nothing is displayed
snapshot = await categoriesCollection.where('ux.index', isEqualTo: 11).where('ux.index', isEqualTo: 3).getDocuments(); 

因为,你要求ux.index == 11 ux.index == 3 这自然不会返回任何内容。

这里

snapshot = await categoriesCollection.where('ux.index', isLessThanOrEqualTo: 11).where('ux.index', isGreaterThan: 11).getDocuments();

因为,你要求ux.index &lt;= 11 ux.index &gt;= 11 这自然不会返回任何内容,因为条件不适用。

满足您需求的潜在解决方案
一、请替换
snapshot = await categoriesCollection.getDocuments();

snapshot = await categoriesCollection.where('ux.index', descending: false).getDocuments();

假设ux.index 从 0 开始并递增直到超过 11。

以后请更换
return categories;
使用下面的代码-sn-p。


List orderedCategories = List();  
// cherry-pick 11 and 3  
orderedCategories.add(categories[11]);  
categories.removeAt(11); 
orderedCategories.add(categories[3]);  
categories.removeAt(3);
// add all the rest
orderedCategories.addAll(categories);  

return orderedCategories;

注意!这是一个可以优化的快速解决方案。

【讨论】:

    猜你喜欢
    • 2020-02-02
    • 2020-03-26
    • 2017-08-10
    • 1970-01-01
    • 2019-05-16
    • 2018-04-04
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多