[英]How do i limit the number of documents from Firebase with dates in Flutter
I am building a flutter app that fetches documents through firebase cloud firestore but i want it to show only the documents written that day.我正在构建一个 Flutter 应用程序,它通过 firebase cloud firestore 获取文档,但我希望它只显示当天编写的文档。 Like if i add to the collection, it will return only the documents for today.
就像我添加到集合中一样,它只会返回今天的文档。 Please help
请帮忙
Container(
height: MediaQuery.of(context).size.height,
child: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('all').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text('.....Loading');
}
return ListView.builder(
scrollDirection: Axis.vertical,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot all = snapshot.data.documents[index];
return Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Column(
children: [
Text('10:00', style: TextStyle(fontSize: 18),),
SizedBox(height: 3,),
Text('05 Sep', style: TextStyle(fontSize: 13),),
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Europa league', style: TextStyle(fontSize: 15),),
SizedBox(height: 2,),
Text( '${all['gg']['home']} vs ${all['gg']['away']}', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),),
you just need to put a where clause on the query like so (this gives anything made in last 24 hours):你只需要像这样在查询上放置一个 where 子句(这给出了过去 24 小时内所做的任何事情):
Firestore.instance
.collection("all")
.where("created_date", isGreaterThan: DateTime.now().subtract(Duration(days: 1)) )
.snapshots();
Make sure that your data has dates, when inserting you can do it like so:确保您的数据具有日期,插入时您可以这样做:
Firestore.instance
.collection("all")
.add({
'data': "data",
'created_date' : FieldValue.serverTimestamp(),
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.