5

FutureBuilder with a Firestore query on a field of Type timestamp comes back with no data in snapshot. However, the same query without the orderBy works just fine.
What am I missing ? Thanks for the help.

// Working code
future: Firestore.instance.collection('messages').where('toid',isEqualTo: _emailID).getDocuments(),
builder: (context, snapshot) ...

// Not Working - returns to if(!snapshot.hasData)
future: Firestore.instance.collection('messages').where('toid',isEqualTo: _emailID).orderBy('_timeStampUTC', descending: true).getDocuments(),
builder: (context, snapshot) ...

enter image description here

AhabLives
  • 1,308
  • 6
  • 22
  • 34

5 Answers5

10

I think you are missing a ' here '_timeStampUTC, so it should be:

 orderBy('_timeStampUTC', descending: true)

EDIT:

Also, you need to be sure to create an index for toid and other for _timeStampUTC, this is done when you try to order by a property that is not in you the where of the query.

Rodrigo Mata
  • 1,779
  • 2
  • 14
  • 23
  • Sorry about that. That was a typo on my part I was typing by memory. I have corrected the question. Thanks. – AhabLives Jul 20 '18 at 03:06
  • Mmm ok, then, have you tried to create [an index](https://firebase.google.com/docs/firestore/query-data/index-overview) for `_timeStampUTC`? – Rodrigo Mata Jul 20 '18 at 03:10
  • just tried the index on the '_timeStampUTC' field - no luck. Thanks. – AhabLives Jul 20 '18 at 03:21
  • Sorry, I'm not that good with Dart, and was looking at [their docs](https://github.com/flutter/plugins/blob/master/packages/cloud_firestore/lib/src/query.dart#L147), and everything looks right... I see you are not [the only one with this problem](https://github.com/flutter/flutter/issues/15928), but the way they resolved it was by creating an index – Rodrigo Mata Jul 20 '18 at 03:51
  • I'm trying your idea again. After some digging I have found you have to put the index on the where field and the orderBy field – AhabLives Jul 20 '18 at 03:57
  • You were correct about the index. However, with to issues -1. the index MUST be created on whatever fields you are querying - 2. indexes MUST be in the order of your query. I had to create the 'toid' index first then the '_timeStampUTC' second...then it worked. Can you update your answer with these points and I will mark as correct. Thanks for the help. – AhabLives Jul 20 '18 at 15:42
  • @AhabLives I am facing exactly same issue, but I am using StreamBuilder. Can you guide how to set up index? I set up CompositeIndex See image: https://imgur.com/iTEY6eX When I add new document, stream builder does not show in ListView where as If I remove orderby part it show live updates/ add / deleted – Wikki Mar 20 '20 at 13:28
  • @Wikki did you solved it? I am also getting the same problem. – Arvina Kori Feb 18 '21 at 09:49
3

For those who are still facing issue and don't know how to add indexes

Click on Add Index from Firebase console: Cloud Firestore indexes section: You need to add collection id, fields and query scope and create Index. It will take some times to build index.

enter image description here

After adding index on fields you will get below with status enabled enter image description here

Nik
  • 1,991
  • 2
  • 13
  • 30
1

Use @ServerTimestamp annotation for _timeStampUTC and try to to orderBy query

To make Firestore automatically generate Timestamp values when we upload data to the database, we must use the annotation @ServerTimestamp and set the value to null.

Rohit Maurya
  • 730
  • 1
  • 9
  • 22
0

yep, it's just missing an index. if you're using firestore.indexes.json then add this:

{
  "collectionId": "messages",
  "fields": [
    { "fieldPath": "toid", "mode": "ASCENDING" },
    { "fieldPath": "_timeStampUTC", "mode": "DESCENDING" }
  ]
}
blaneyneil
  • 3,122
  • 13
  • 14
0

write your query:-

Firestore.instance.collection('messages').where('toid',isEqualTo: _emailID).orderBy('_timeStampUTC', descending: true).getDocuments()

after this, in your log, a link will be generated. it will soething like this https://console.firebase.google.com/v1/r/project............ follow that link to create index for your query after creating the index reload your app and now you will be able to fetch all your collections. :)

JideGuru
  • 7,102
  • 6
  • 26
  • 48