从Firestore数据库中获取1周的数据,使用Flutter。

huangapple go评论84阅读模式
英文:

Getting 1 week data from firestore database with flutter

问题

I want to pull last 1 week data from firestore database
but the data is not coming, where, what did I do, even I don't know, can you help me?

DateTime oneWeekAgo = DateTime.now().subtract(Duration(days: 7));
Timestamp oneWeekAgoTimestamp = Timestamp.fromDate(oneWeekAgo);

QuerySnapshot snapshot = await FirebaseFirestore.instance
    .collection('ogrenciler').where('tc', isEqualTo: widget.tc)
    .where('tarih', isGreaterThanOrEqualTo: oneWeekAgoTimestamp)
    .orderBy('tarih', descending: true).get();

if (snapshot.docs.isNotEmpty) {
    for (QueryDocumentSnapshot documentSnapshot in snapshot.docs) {
        print(documentSnapshot.data());
    }
} else {
    print('Veri yok');
}
英文:

从Firestore数据库中获取1周的数据,使用Flutter。

I want to pull last 1 week data from firestore database
but the data is not coming, where, what did I do, even I don't know, can you help me?

DateTime birHaftaOnce = DateTime.now().subtract(Duration(days: 7));
Timestamp birHaftaOnceTimestamp = Timestamp.fromDate(birHaftaOnce);

QuerySnapshot snapshot = await FirebaseFirestore.instance
    .collection('ogrenciler').where('tc', isEqualTo: widget.tc)
    .where('tarih', isGreaterThanOrEqualTo: birHaftaOnceTimestamp)
     .orderBy('tarih', descending: true).get();
      
      if (snapshot.docs.isNotEmpty) {
        for (QueryDocumentSnapshot documentSnapshot in snapshot.docs) {
            print(documentSnapshot.data());
        }
      } else {
          print('Veri yok');
      }

答案1

得分: 1

你的查询是根据文档的“tarih”字段的值进行排序的。但是,根据你分享的屏幕截图,文档的根部没有“tarih”字段。

相反,有一个“gunlukDers”数组字段,其中每个单独的数组元素都有一个“tarih”值。但你想要查询这样的子字段。

解决方法是为你的每个文档添加一个顶层字段,指示日期,然后在该字段上进行查询。

另一种方法是为你现在展示的每个文档创建一个名为“gunlukDers”的子集合,其中存储数组中现在存储的数据,然后在该子集合上进行查询。

英文:

Your query is ordering the documents by the value of their tarih field. But looking at the screenshot you shared there is no tarih field in the root of the document.

Instead there is an gunlukDers array field, where each individual array element has a tarih value. But you want query for such a subfield.

The solution is to add a top-level field that indicates the date to each of your documents, and then query on that.

An alternative would be to give each of the documents you show now a gunlukDers subcollection with the data you now store in the array and then query that.

huangapple
  • 本文由 发表于 2023年2月26日 22:40:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定