Firestore查询中的where和orderBy在Flutter应用程序中用于流时返回null。

huangapple go评论62阅读模式
英文:

Firestore query with where and orderBy returning null when using in Streams in Flutter App

问题

I am trying to get some document and sort them but when I am doing it with .orderBy it is returning null. If I use only .where and don't use orderBy, it is working fine.

我正在尝试获取一些文档并对它们进行排序,但当我使用.orderBy时,它返回null。如果只使用.where而不使用orderBy,它可以正常工作。

I want to get some documents in a collection name 'expenses' sorted with date. But when I am doing it with .where and .orderBy it is returning null. If I use only .where and don't use orderBy, it is working fine.

我想获取一个名为'expenses'的集合中按日期排序的一些文档。但是,当我使用.where和.orderBy时,它返回null。如果只使用.where而不使用orderBy,它可以正常工作。

    UserModel user;

    return firestore
        .collection('expenses')
        .where("usersList", arrayContains: auth.currentUser?.phoneNumber)
        .snapshots()
        .map((event) {
      List<Expense> expenseList = [];
      for (var i in event.docs) {
        expenseList.add(Expense.fromMap(i.data()));
      }

      return expenseList;
    });
// It worked correctly

上面的代码在不排序的情况下正常工作。

    UserModel user;

    return firestore
        .collection('expenses')
        .where("usersList", arrayContains: auth.currentUser?.phoneNumber)
        .orderBy("date", descending: true)
        .snapshots()
        .map((event) {
      List<Expense> expenseList = [];
      for (var i in event.docs) {
        expenseList.add(Expense.fromMap(i.data()));
      }

      return expenseList;
    });

这段代码返回null。

And as I can see in my App the function fetched data for a second and then returned null.

而且我可以在我的应用程序中看到,该函数在获取数据一秒钟后返回null。

英文:

I am trying to get some document and sort them but when I am doing it with .orderBy it is returning null. If I use only .where and don't use orderBy, it is working fine.

I want to get some documents in a collection name 'expenses' sorted with date. But when I am doing it with .where and .orderBy it is returning null. If I use only .where and don't use orderBy, it is working fine.

Stream&lt;List&lt;Expense&gt;&gt; getExpenseStream() {
    UserModel user;

    return firestore
        .collection(&#39;expenses&#39;)
        .where(&quot;usersList&quot;, arrayContains: auth.currentUser?.phoneNumber)
        .snapshots()
        .map((event) {
      List&lt;Expense&gt; expenseList = [];
      for (var i in event.docs) {
        expenseList.add(Expense.fromMap(i.data()));
      }

      return expenseList;
    });
// It worked correctly

The above code works perfect without sorting.

Stream&lt;List&lt;Expense&gt;&gt; getExpenseStream() {
    UserModel user;

    return firestore
        .collection(&#39;expenses&#39;)
        .where(&quot;usersList&quot;, arrayContains: auth.currentUser?.phoneNumber)
        .orderBy(&quot;date&quot;, descending: true)
        .snapshots()
        .map((event) {
      List&lt;Expense&gt; expenseList = [];
      for (var i in event.docs) {
        expenseList.add(Expense.fromMap(i.data()));
      }

      return expenseList;
    });

This code is returning null.

And as I can see in my App the function fetched data for a second and then returned null.

Firestore查询中的where和orderBy在Flutter应用程序中用于流时返回null。

答案1

得分: 1

如果问题在添加orderBy指令时开始出现,很可能是因为此时Firestore需要一个复合索引来执行查询,而不像单字段索引一样,你必须自己明确地创建它。

你可以尝试捕获并记录访问数据库的代码中的错误,因为通常缺少索引会导致错误消息,该错误消息包含指向Firestore仪表板的链接,其中已经填写了所需索引的所有必要信息。有关更多信息,请参阅关于通过错误消息创建缺少索引的文档。

或者,你可以自己创建一个包含所需字段的复合索引。在这种情况下,你需要首先按升序添加usersList字段,然后按降序添加date字段。有关更多信息,请参阅关于手动创建缺少索引的文档。

英文:

If the problem starts when you add the orderBy instruction, it's likely because at that point Firestore needs a composite index to perform the query and (unlike single-field indexes) you have to create that explicitly yourself.

You might want to try catching and logging errors from the code accessing the database, as a missing index typically should give you an error message and that error message includes a link to the Firestore dashboard with all the necessary info for the index filled in already. For more on this, also see the documentation on creating a missing index through an error message.

Alternatively, you can create a new composite index with the necessary fields yourself. In that case you'll need an to first add the usersList field in ascending order, and then the date field in descending order. For more on this, also see the documentation on creating a missing index manually

huangapple
  • 本文由 发表于 2023年3月21日 00:43:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定