繁体   English   中英

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

[英]How do i limit the number of documents from Firebase with dates in Flutter

我正在构建一个 Flutter 应用程序,它通过 firebase cloud firestore 获取文档,但我希望它只显示当天编写的文档。 就像我添加到集合中一样,它只会返回今天的文档。 请帮忙

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),),

你只需要像这样在查询上放置一个 where 子句(这给出了过去 24 小时内所做的任何事情):

Firestore.instance
  .collection("all")
  .where("created_date", isGreaterThan: DateTime.now().subtract(Duration(days: 1)) )
  .snapshots();

确保您的数据具有日期,插入时您可以这样做:

Firestore.instance
  .collection("all")
  .add({
    'data': "data",
    'created_date' : FieldValue.serverTimestamp(),
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM