Home > Net >  Unhandled Exception: FormatException: Trying to read yyyy from null at position 0
Unhandled Exception: FormatException: Trying to read yyyy from null at position 0

Time:03-08

I tried to use dateTime to open or close the access to the registration form, I saved the value of open or close date in my Firebase.. so I must retrieve the date value from the database, and compare with the user current time. To get the country current time, I used WorldTime API.. Please can you check my code to let me know I dit a mistake or error ?

Thanks a lot

    await FirebaseFirestore.instance
        .collection("DateTimeActivity")
        .doc("OpenTime")
        .snapshots()
        .listen((event) {
      var openDateTime = event.get("OpenTime");
      var closeDateTime = event.get("closeTime");

      DateFormat myFormat = DateFormat('yyyy-MM-dd');

      DateTime open = myFormat.parse(openDateTime);
      DateTime close = myFormat.parse(closeDateTime);
      //DateTime current = myFormat.parse(currentTime);

      if (DateTime.parse(currentTime).isBefore(open)) {
        isTime = false;
      } else if (DateTime.parse(currentTime).isAfter(close)) {
        isTime = false;
      } else {
        isTime = true;
      }
    });

    return isTime;
  }  ```

CodePudding user response:

even thought you set a date in firebase it will store it as a timestamp. try: myFormat.parse((openDateTime as TimeStamp).toDate())

CodePudding user response:

Hope these links help you: unable to convert string date in Format yyyyMMddHHmmss to DateTime dart, Error with DateFormat in dart: "Trying to read -YYYY from 17-04-2020 at position 10".

If my answer is helpful to you, please accept my answer.

  • Related