Home > OS >  how we can retrieve only previous one day data from Firestore in Flutter?
how we can retrieve only previous one day data from Firestore in Flutter?

Time:01-19

FirebaseFirestore.instance
.collection("Orders")
.where('ostatus', isEqualTo: "4")
.where('vid', isEqualTo: uid)
.orderBy("latestorder", descending: true)
.snapshots(),

Which change can i do in this code to get only previous one day data from firebase firestore flutter?

CodePudding user response:

I tried it through this

FirebaseFirestore.instance
            .collection("Orders")
            .where('ostatus', isEqualTo: "4")
            .where('vid', isEqualTo: uid)
            .where("latestorder",
                isGreaterThanOrEqualTo:
                    Timestamp.now().toDate().subtract(Duration(days: 1)))
            .orderBy("latestorder", descending: true)
            .snapshots(),

CodePudding user response:

you can use the below format to get a previous day, but you have to add a timestamp while the data is inserting data into firestore

  DateTime todaysDate = DateTime.now();
  DateTime yd = DateTime.utc(todaysDate.year, todaysDate.month, todaysDate.day -1);

If you want to only yest data :

                  await FirebaseFirestore
                              .instance
                              .collection('Orders')
                              .where('ostatus', isEqualTo: "4")
                              .where('timestamp', isEqualTo: yd)
                              .get();

Another is if you want to data in range formate

                await FirebaseFirestore
                          .instance
                          .collection('Orders')
                          .where('timestamp', isGreaterThanOrEqualTo: yd)
                          .where('timestamp', isLessThanOrEqualTo: td)
                          .get();
  • Related