0

This is my search code. I am using this code for get Card from firestore. I am trying to set sorting by date my cards.

How can I do that. Thanks for help.

class SearchService {
      List<Future<QuerySnapshot>> searchByName() {
        return [
          Firestore.instance
              .collection('dis')
              .where('no')
              .getDocuments(),
    
    
        ];
      }
    }
4
  • What is 'no' in you're query? Btw look at this for queries in flutter firestore: Flutter Firestore stackoverflow.com/questions/48937864/… Commented Sep 26, 2020 at 16:36
  • its string number like 123456 Commented Sep 26, 2020 at 16:37
  • So how in this, you are sorting according to date?? Commented Sep 26, 2020 at 16:38
  • I can change 'no' to timestamp but I am still dont know how can I sort by date Commented Sep 26, 2020 at 16:40

1 Answer 1

3

A "where" clause isn't going to sort the query results for you. You will need to use orderBy for that. You will also need to know the name of the date field to use for ordering, as shown in the documentation.

          Firestore.instance
              .collection('dis')
              .orderBy('date')
              .getDocuments(),

If you don't have a field that stores a date for each document, you won't be able to order the results.

Sign up to request clarification or add additional context in comments.

2 Comments

Moreover, if run into index error, you need to create an index on your own in firebase.
you can change the order of sort by adding orderBy('date', descending: true),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.