FormatException(FormatException:无效的日期格式)

FormatException (FormatException: Invalid date format)

看来我的字符串不是解析 DateTime 的有效格式我似乎无法理解罪魁祸首在哪里。 当应用程序启动数据时,会不时发生这种情况。

错误代码:

  initListOfUsageValues() async {
    if (favoriteBike != null) {
      int periodWanted;
      if (displayedUsageType == "year") {
        periodWanted = displayedYear;
      } else if (displayedUsageType == "month") {
        periodWanted = displayedMonth;
      } else {
        periodWanted = displayedWeek;
      }
      List<int> list = await APIBike().getUsageData(
        jwt: _user!.jwt,
        bikeId: favoriteBike!.id,
        year: displayedYear,
        duration: displayedUsageType,
        periodWanted: periodWanted,
      );
      //The received list is in seconds so we need to convert it in minute
      list = convertSecondsToMinute(list);
      setState(() {
        listOfUsageValues = list;
      });
    } else {
      print("favoriteBike null in initListOfUsageValues");
      //check if the alertdialog saying that there is no bike linked to this account yet has been displayed already or no
      final storage = new FlutterSecureStorage();
      String lastNoBikeAlertShowUp =
          await storage.read(key: "last_no-bike-alert_show-up") ?? "";
      //If there is more than 60 minutes since last show up, we show the alertdialog saying that there is no bike linked to this account yet, else we don't
      DateTime lastNoBikeAlertShowUpDateTime =
          DateTime.parse(lastNoBikeAlertShowUp);
      var now = new DateTime.now();
      int timeInMinuteSinceLastAlertShowUp =
          now.difference(lastNoBikeAlertShowUpDateTime).inMinutes;
      if (timeInMinuteSinceLastAlertShowUp > 60) {
        await storage.write(
            key: "last_no-bike-alert_show-up", value: now.toString());
        showDialog(
            context: context,
            builder: (BuildContext context) {
              return noBikeYetAlertDialog(context);
            });
      }
    }
  }

此行触发错误:

      DateTime lastNoBikeAlertShowUpDateTime =
          DateTime.parse(lastNoBikeAlertShowUp);

lastNoBikeAlertShowUp 为空字符串。因为您在 date_time.dart

中看到了 FormatException 中断
// set default date or check if it is not empty
String lastNoBikeAlertShowUp = await storage.read(key: "last_no-bike-alert_show-up") ?? "";
if(lastNoBikeAlertShowUp.isNotEmpty){

}