首页 > 解决方案 > 使用 Firebase 中的 StartAt 和 Flutter 时间戳

问题描述

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

我以这种方式保存帖子:

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

并尝试以这种方式查询:

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

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

希望使用这种类型的查询获得帮助:)

标签: firebasedartfluttergoogle-cloud-firestore

解决方案


将 DateTime 转换为 Unix 日期时间

'timestamp': DateTime.now().toUtc().millisecondsSinceEpoch

推荐阅读