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