3

How do I get the most recent first from Firestore?

FirebaseFirestore.instance.collection(Strings.factsText)
                .where(
                    Strings.category,
                    whereIn: [Strings.climateChange])
                    .orderBy('timestamp', descending: true)
                    .snapshots(),
               
                    ...
                  final factsDocs = snapshot.data.documents;
                  return FactsListContainer(factsDocs: factsDocs);
                });

The issue appears to be with .where when using with .orderBy!

SK1dev
  • 1,049
  • 1
  • 20
  • 52
  • Hi. you don't have a field with Date? or a field with alphabetic order ? If yes you can order your data with .orderBy("date", Query.Direction.DESCENDING) and just get the first one with .limit(1) – ande Nov 25 '20 at 15:11
  • @ande Hi, I tried adding a createdAt field but when I tried to incorporate orderBy with my code I couldn't get it to work. Could you show me how it would work with the my code provided here please? – SK1dev Nov 25 '20 at 15:28
  • I'm not an expert in flutter ( firestore more ) but you can see https://stackoverflow.com/questions/58154176/how-to-order-data-from-firestore-in-flutter-orderby-not-ordering-correct and after add .limit(1) of your query. For me it'll be .collection(Strings.factsText).where(...).orderBy("createdAt", descending: true).limit(1).snapshots() – ande Nov 25 '20 at 15:38
  • @ande Thanks. This is what I tried before but I get an error: The getter 'documents' was called on null – SK1dev Nov 25 '20 at 15:54
  • Are you sure of your "Strings.factsText" is a collection ? – ande Nov 25 '20 at 15:57
  • @ande Yes it is and its all been working perfectly fine. I can get all the documents its just that I want to order them with the most recently added first. – SK1dev Nov 25 '20 at 16:01
  • I don't think it's the query and I can't help with flutter , but I found this : https://stackoverflow.com/questions/55553839/how-to-fix-getter-documents-was-called-on-null-in-flutter – ande Nov 25 '20 at 16:03
  • @ande Thanks, I'll take a look – SK1dev Nov 25 '20 at 16:15
  • I think "if(snap.data == null) return CircularProgressIndicator();" in your builder method. It' s a correct answer, because you read values but the query doesn't end successfully. You don't have this problem with other query because it's almost "synchronous". After that I think I can't help you more. – ande Nov 25 '20 at 16:24
  • @ande I added this too but then it just stays on the CircularProgressIndicator. Thanks for your help – SK1dev Nov 25 '20 at 18:09

2 Answers2

6

If you have a createdAt field with a timestamp, or an otherwise always incrementing value, you can get the snapshots in descending order of that field with:

stream: FirebaseFirestore.instance
  .collection(Strings.factsText)
  .orderBy('timestamp', descending: true)
  .where(
    Strings.category,
    whereIn: [Strings.climateChange])
    .snapshots(),

Also see the FlutterFire documentation on the Query.orderBy method.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

I finally solved it by creating an index in the Firebase console and this worked perfectly so now I'm getting the latest first.

In my database I went to Indexes to create a new index, added my collection name, and then for the fields I added 'category' - descending and 'timestamp' - descending, clicked Collection and then Create Index.

SK1dev
  • 1,049
  • 1
  • 20
  • 52
  • I am not getting any request / error in vs code editor to create `index`. How can we delete already created `indexes` on Firestore? any suggestion? Thanks a lot. – Kamlesh Jun 20 '21 at 09:43