使用 Firebase 的 StartAt 和 Flutter 时间戳

Fel*_*sar 7 dart firebase flutter google-cloud-firestore

我正在使用时间戳属性在 Firestore 上保存一些帖子,然后查询按时间戳排序的帖子。我正在尝试创建一个“loadMore”函数,然后在用户滚动到底部并加载更多帖子时起作用。

我以这种方式保存帖子:

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

并尝试以这种方式查询:

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

但是,它一直忽略“startAfter”并返回一个从集合的第一个元素而不是下一个元素开始的列表。

很想获得使用此类查询的帮助:)

Gün*_*uer 11

将日期时间转换为 Unix 日期时间

'timestamp': DateTime.now().toUtc().millisecondsSinceEpoch
Run Code Online (Sandbox Code Playgroud)

  • 有效。我更喜欢使用 DateTime 格式以更友好的方式查看数据库中的数据,但是,我现在可以接受。要检索我正在使用的数据:`DateTime.fromMillisecondsSinceEpoch(doc.data['timestamp'], isUtc: true),` (4认同)
  • Firebase 和 Firestore 使用 unix 日期时间格式(自纪元以来的毫秒数),Firebase 客户端无法知道何时转换,因此您必须小心自己。您要检索的代码看起来不错。 (3认同)