-2

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),),
Peters Victor
  • 70
  • 1
  • 9
  • Use a Timestamp? – lenz Aug 25 '20 at 07:20
  • Does this answer your question? [Add timestamp in Firestore documents](https://stackoverflow.com/questions/51846914/add-timestamp-in-firestore-documents) – lenz Aug 25 '20 at 07:23

1 Answers1

0

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(),
  });
Jan
  • 1,332
  • 9
  • 13
  • I'm a complete newbie and i don't really understand – Peters Victor Aug 25 '20 at 10:27
  • Thank you Jan but i tried it and uploaded a new document but the database did not return anything – Peters Victor Aug 25 '20 at 10:44
  • I want a situation where i upload a document to my collection and it stays on the screen for throughout the day Example, if i upload on a monday morning, it will only stay throughout monday till i write another document for tuesday – Peters Victor Aug 25 '20 at 11:18
  • will i have a timestamp field in my cloud firestore documents? – Peters Victor Aug 25 '20 at 11:27
  • Yes you'll need a timestamp field in your cloud firestore documents. When you add a document, make sure you give it a date, in my example i called the field 'created_date' and instructed firestore to set the value to the servers current timestamp. When you go to retrieve your documents, you need to select from a collection (which you've done) and then add a 'where clause' that will filter the results to the specific date range. In my example it is filtering every document in the collection that has a 'created_date' with a value greater than (newer) 1 day from this exact time (aka 24 hours). – Jan Aug 26 '20 at 02:59