简体   繁体   English

使用 flutter 中的时间戳或 firebase 服务器时间从 firestore 保存和查询数据

[英]save and query data from firestore with timeStamp or firebase server time in flutter

Please I have a history page on my flutter app that I want to work on and I have been thinking of a way to store the data correctly in my firestore so that I can achieve the desired result as seen in the picture below请在我的 flutter 应用程序上有一个我想要处理的历史页面,我一直在考虑一种方法将数据正确存储在我的 firestore 中,以便我可以获得如下图所示的预期结果

这是图片

I would love to have data of thesame day stored in thesame container as seen in the picture above.我希望将同一天的数据存储在同一个容器中,如上图所示。 My challenge is, I do not know how to structure my data to get the desired result.我的挑战是,我不知道如何构建我的数据以获得预期的结果。

here is what I have tried;这是我试过的;

my Count class file is below although I am not sure if that's what I will really do.我的计数 class 文件在下面,但我不确定这是否是我真正要做的。

class Count {
 String id;
 final int count;
 final createdOn;

 Count({this.id = '', required this.count, required this.createdOn});

 Map<String, dynamic> toJson() =>
     {'id': id, "count": count, "createdOn": createdOn};

 Count.fromSnapShot(DocumentSnapshot<Map<String, dynamic>> snapshot)
     : id = snapshot.id,
       count = snapshot.data()!["count"],
       createdOn = snapshot.data()!["createdOn"];
}

and this is where I send data to firestore using onpressed in button这是我使用 onpressed in 按钮将数据发送到 firestore 的地方

 onPressed: () async {
                               exerciseCounter.increment();
                               final counter = exerciseCounter.count;

                               final FirebaseAuth auth = FirebaseAuth.instance;

                               final User? user = await auth.currentUser;

                               final uid = user?.uid;

                               final percents = FirebaseFirestore.instance
                                   .collection('exercise-percentage')
                                   .doc(uid)
                                   .collection("daily-percentage");

                               final percent = Count(
                                   count: counter,
                                   createdOn: FieldValue.serverTimestamp());

                               final json = percent.toJson();

                               await percents.add(json);
                             },

now I am not sure of the correct way to use streambuilder to get the data from firestore and join the data of thesame day in thesame container.现在我不确定使用 streambuilder 从 firestore 获取数据并将同一天的数据加入同一容器中的正确方法。 I understand that I will need to use query method to query my data with the firestore serverTimeStamp but I don't know how to use it to fetch data of thesame day and display it as shown in the picture above.我知道我需要使用查询方法通过 firestore serverTimeStamp 查询我的数据,但我不知道如何使用它来获取当天的数据并如上图所示显示它。

I will appreciate it if someone can really help me out.如果有人真的能帮助我,我将不胜感激。 It can just be with a simple example I can follow or by correcting and adding to my code.它可以只是一个我可以遵循的简单示例,也可以是通过更正和添加到我的代码中。 Thank you for your time.感谢您的时间。

I would love to have data of the same day stored in the same container as seen in the picture above.我希望将同一天的数据存储在同一个容器中,如上图所示。

You could have an extra field beside the createdOn one which holds the date in the following format YYYYMMDD.您可以在createdOn旁边有一个额外的字段,它以以下格式保存日期 YYYYMMDD。

This way, you can easily:这样,您可以轻松地:

  • Query the Firestore document for a specific day查询特定日期的 Firestore 文档
  • Use a set of count aggregation queries to calculate the KPIs/Counters for a specific day使用一组计数聚合查询来计算特定日期的 KPI/计数器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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