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<List<Expense>> getExpenseStream() {
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
The above code works perfect without sorting.
Stream<List<Expense>> getExpenseStream() {
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;
});
This code is returning null.
And as I can see in my App the function fetched data for a second and then returned null.