0

I am trying to create simple Firebase Firestore query that allows the user to view the top forums from the previous day at most. This is the query I am currently using inside a ```StreamBuilder``` in Flutter.
_stream = FirebaseFirestore
    .instance
    .collection('forums')
    .where(
      'forumTimestamp',
      isGreaterThanOrEqualTo: DateTime
              .now()
          .subtract(
            const Duration(
                days: 1),
          )
          .millisecondsSinceEpoch,
    )
    .orderBy(
      'forumTotalLikes',
      descending: true,
    )
    .snapshots();

This should return documents in my Firebase collection that were posted not more than a day ago, and then order them by the number of "forumTotalLikes," it has, to show the top posts from not more than a day ago. For context, this code changes a Streambuilder's stream when a user presses on a button, similar to reddit. When the user presses the button, and changes _stream to the above code, I get this error.

════════ Exception caught by gesture ═══════════════════════════════════════════
The following assertion was thrown while handling a gesture:
The initial orderBy() field "[[FieldPath([forumTotalLikes]), true]][0][0]" has to be the same as the where() field parameter "FieldPath([forumTimestamp])" when an inequality operator is invoked.
'package:cloud_firestore/src/query.dart':
query.dart:1
Failed assertion: line 485 pos 13: 'conditionField == orders[0][0]'

When I add

.orderBy('forumTimestamp')

Right of the comparision query, it loads, but doesn't correctly sort.

_stream = FirebaseFirestore
    .instance
    .collection('forums')
    .where(
      'forumTimestamp',
      isGreaterThanOrEqualTo:
          DateTime.now()
              .subtract(
                const Duration(
                    days: 1),
              )
              .millisecondsSinceEpoch,
    )
    .orderBy('forumTimestamp')
    .orderBy(
      'forumTotalLikes',
      descending: true,
    )

It only sorts forumTimestamp, but ignores the forumTotalLikes field. Note that there are no trivial mistakes, such as comparing invalid types. Whenever I remove the .where query entirely, it does correctly sort by the most liked forums. I just wan't to be able to get documents posted from not more than a day ago, and sort them by forumTotalLikes

Here for reassurance: enter image description here

  • In that last snippet, since you first order on `forumTimestamp` and then on `forumTotalLikes`, the latter will only matter for documents where the `forumTimestamp` value is the same. – Frank van Puffelen Feb 05 '23 at 21:47
  • The error message told me that I had to order by ```forumTimestamp``` or else it wouldn't run. How would I get the query to run without sorting by ```forumTimestamp``` entirely, just using it in a comparision operator? – Mohammad Abd-Elmoniem Feb 05 '23 at 21:57
  • ```The initial orderBy() field "[[FieldPath([forumTotalLikes]), true]][0][0]" has to be the same as the where() field parameter "FieldPath([forumTimestamp])"``` – Mohammad Abd-Elmoniem Feb 05 '23 at 21:58
  • You can't use a relational comparison on a field without first sorting on that field. If you want to display the documents on `forumTotalLikes`, you will have to reorder them in your application code. – Frank van Puffelen Feb 06 '23 at 01:22
  • So I gather it's not currently possible to pass this query as a stream, instead I'll need to add other code? – Mohammad Abd-Elmoniem Feb 06 '23 at 02:25

0 Answers0