It seems that my string is not a valid format to parse the DateTime i can't seem to understand where the culprit is. This happen from time to time when the app initiate the data.
error code :
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);
});
}
}
}
The error is trigered in this line :
DateTime lastNoBikeAlertShowUpDateTime =
DateTime.parse(lastNoBikeAlertShowUp);
CodePudding user response:
lastNoBikeAlertShowUp is empty string. Because you see break on trow FormatException inside of date_time.dart
// 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){
}