Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using StartAt from Firebase with Flutter Timestamps

I'm saving some posts on Firestore with a timestamp property and then I query this posts ordered by timestamp. I'm trying to create a "loadMore" function then works when the user scrolls to bottom and loads more posts.

I'm saving the posts this way:

Firestore.instance
  .collection('posts')
  .add(<String, dynamic> {
     'content': 'content,
     'likes': 0,
     'comments': 0,
     'timestamp': DateTime.now(),
   })

And trying to query this way:

Firestore.instance
  .collection('posts')
  .orderBy('timestamp', descending: true)
  .startAfter(
    []..add(posts.last.timestamp)
  )
  ..limit(5)

But, it keeps ignoring the "startAfter" and returning me a list starting with the first element of the collection and not with the next ones.

Would love to get help using this type of query :)

like image 544
Felipe César Avatar asked Mar 06 '19 14:03

Felipe César


1 Answers

Convert the DateTime to Unix date-time

'timestamp': DateTime.now().toUtc().millisecondsSinceEpoch
like image 91
Günter Zöchbauer Avatar answered Oct 02 '22 08:10

Günter Zöchbauer