简体   繁体   English

如何从 flutter 的 firestore 中的一个字段发送和接收两种类型的数据?

[英]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.

相关问题 我如何从 flutter firestore 中的 querysnapshot 获取单个字段值 - How do i get the single field value from querysnapshot in flutter firestore 如何从 Firestore 和 map 接收数据到一个元素? - How to receive data from Firestore and map it to an element? 如何在 Firebase Firestore 上获取一个字段? - How can I get one field on Firebase Firestore? 如何将来自 Firestore 的字段值存储在 Flutter 中? - How to store field value from firestore in Flutter? 我可以使用 WebSocket 协议使用 ESP32S3 使用 ESP-IDF C 从 Cloud Firestore 发送和接收数据吗 - Can I use a WebSocket protocol to send and receive data from Cloud Firestore using an ESP32S3 Using ESP-IDF C flutter:从 firestore 的数组字段中获取用户列表及其数据 - flutter: getting a list of users and their data from the array field in firestore 如何从 cloud-firestore 中的集合接收所有文档对象? - How can I receive all document objects from a collection in cloud-firestore? 如何在 flutter 中没有未来构建器的情况下从 Firestore 获取单个用户数据 - How to get a single user data from firestore without future builder in flutter 如何从 Dropdownlist 中的 firestore 接收数组的 Map 数据值 - How to receive Map data value of an array from firestore inside Dropdownlist 无法从 flutter Cloud firestore 获取一份文件 - Can't get one document from flutter cloud firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM