简体   繁体   English

我如何限制 Firebase 中带有 Flutter 日期的文档数量

[英]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.

相关问题 MYSQL 如何从表中选择所有电子邮件但限制具有相同域的电子邮件数量 - MYSQL How do I Select all emails from a table but limit number of emails with the same domain Flutter (Firebase) 约会应用,来自同一集合的文档之间的关系 - Flutter (Firebase) dating app, relationships between documents from the same collection MongooseJS - 限制子文档中的文档数 - MongooseJS - limit number of documents in subdocument 如何将数据字段从一个集合中的所有文档移到另一集合中的新文档中? - How do I move fields of data from all documents in one collection into new documents of a different collection? 使用聚合时如何在运行限制之前获取记录总数 - How do I get the total number of records before I run limit when using Aggregation 如何获取 Firebase 集合中的所有文档(包括不存在的祖先文档)? - How do you fetch all documents (including non-existent ancestor documents) in a Firebase collection? 如何限制表格行数? - How would I limit the number of table rows? Firebase 数据库 - 如何从文档查询中提取元素? - Firebase Database - How to extract elements from a documents query? 我们如何从 firebase 中识别用户并在 flutter 应用程序中根据用户自定义用户体验? - How do we identify users from firebase and customize the user experience based on the user in a flutter app? 如何从flutter中的firebase数据填充列表视图 - How to populate listview from firebase data in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM