Flutter + Firebase.按时间顺序对数据进行排序 [英] Flutter + Firebase. Sorting data in chronological order

查看:50
本文介绍了Flutter + Firebase.按时间顺序对数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些数据添加到集合中.当我检索它时,它按生成的ID排序.是否有可能使Firebase按时间顺序对其进行排序,还是需要再添加一个带有日期的字段并在扑扑中对其进行排序后再对其进行排序?

I add some data to the collection. When i retrive it, it is sorted by that generated id. Is it posible to make firebase to sort it in chronological order or it is required to add one more field with date and sort it once it is retrived in flutter?

推荐答案

是的,您需要一个 createdAt 时间戳字段,并像

Yes you need a createdAt timestamp field and use it like

Firestore.instance
     .collection("users")
     .orderBy('createdAt', descending: true or false).getDocuments()

然后您可以将 createdAt Timestamp (包含在 cloud_firestore 中)存储在颤振侧,并且可以使用获得当前时间戳> Timestamp.now()

And you can store createdAt on flutter side with Timestamp (Its included in cloud_firestore), and you can get current timestamp with Timestamp.now()

这篇关于Flutter + Firebase.按时间顺序对数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆