[英]How can I send and receive two types of data from one single field in firestore for flutter?
I have two textfields in my flutter app.我的 flutter 应用程序中有两个文本字段。 I used showDatePicker on the first one when tapped and showTimePicker on the second one when tapped (to let a user choose a certain date and time).我在第一个点击时使用 showDatePicker,在第二个点击时使用 showTimePicker(让用户选择特定的日期和时间)。
I am using a class model to send and receive date and time both from one variable:我正在使用 class model 从一个变量发送和接收日期和时间:
DateTime? taskDate; // Time as well
I thought about sending them separately but look what happens in firebase when I send them both together:我考虑过将它们分开发送,但看看当我将它们一起发送时 firebase 中会发生什么:
One timestamp field that has date and time seperated ( like a map).一个日期和时间分隔的时间戳字段(如地图)。
My question is how can I point to one of them ( date or time) in Firestore so I can do some actions on them?我的问题是如何在 Firestore 中指向其中一个(日期或时间)以便我可以对它们执行一些操作?
For example I want to add a task in 13 January 20203 and I want this to be saved in date field and the time to be saved in time field.例如,我想在 20203 年 1 月 13 日添加一个任务,我希望将其保存在日期字段中,将时间保存在时间字段中。 What you see in the picture isn't picked date and time.您在图片中看到的不是选择的日期和时间。
This is the code of sending data using a model and bloc state managment:这是使用 model 和 bloc state 管理发送数据的代码:
TaskModel? taskModel;
void addTask({
required String title,
required DateTime date,
}) {
emit(AddPostLoadingState());
taskModel = TaskModel(
taskTitle: title,
taskDate: date,
name: userModel!.name,
uid: userModel!.uId);
FirebaseFirestore.instance
.collection('tasks')
.add(taskModel!.toJson())
.then((value) {
getTask();
}).catchError((error) {
print(error);
emit(AddPostErrorState(error));
});
}
And since I know that datetime recieved as timestamp in firestore.. I changed it in the type when I get data:因为我知道日期时间在 firestore 中作为时间戳收到。我在获取数据时在类型中更改了它:
void getTask() {
emit(GetTaskLoadingState());
FirebaseFirestore.instance
.collection('tasks')
.where('uid', isEqualTo: uId)
.orderBy('taskDate')
.get()
.then((value) {
tasks = []; // Global List<TaskModel>
for (var element in value.docs) {
data['taskDate'] = DateTime.fromMillisecondsSinceEpoch(
data['taskDate'].seconds * 1000);
tasks.add(TaskModel.fromJson(data));
}
emit(GetTaskSuccessState());
}).catchError((error) {
print(error.toString());
emit(GetTaskErrorState(error: error));
});
}
The Firestore Timestamp
field is an atomic value that contains both date and time. Firestore Timestamp
字段是一个包含日期和时间的原子值。 There is no way to set the date and time of the field separately, as it's stored as a single value in the database.无法单独设置字段的日期和时间,因为它作为单个值存储在数据库中。
If you want to manipulate the date and time separate in the database, consider storing them as separate fields (eg in a map as you already describe) yourself.如果您想在数据库中单独操作日期和时间,请考虑自己将它们存储为单独的字段(例如,在您已经描述的 map 中)。 Then you'll need to convert to/from a DateTime
in your code when reading from/writing to the database.然后,在读取/写入数据库时,您需要在代码中与DateTime
进行转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.