简体   繁体   中英

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. 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):

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(),
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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