看來我的字串不是決議 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);
uj5u.com熱心網友回復:
lastNoBikeAlertShowUp 是空字串。因為您在 date_time.dart 的 trow FormatException 上看到了 break
// 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){
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/484540.html
