firebase 我们如何在Flutter中从Firestore仅检索前一天的数据?

6ju8rftf  于 2023-01-21  发布在  Flutter
关注(0)|答案(2)|浏览(144)
FirebaseFirestore.instance
.collection("Orders")
.where('ostatus', isEqualTo: "4")
.where('vid', isEqualTo: uid)
.orderBy("latestorder", descending: true)
.snapshots(),

我可以在这段代码中做什么改变来从firebase firestore flutter中只获取前一天的数据?

xdnvmnnf

xdnvmnnf1#

我试过了

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

slhcrj9b2#

你可以使用下面的格式来获取前一天,但是你必须在数据插入到firestore时添加时间戳

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

如果只想查看昨天的数据:

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

另一种情况是如果你想把数据格式化

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

相关问题