How to handle this error if my API output is "datePosted": ""
Output when the data isNot = ''.
CodePudding user response:
You can do something like this
void main() {
String date = 'invalid-date';
String dormattedDateTime = formatDate(date);
}
String formatDate(String date) {
try {
DateTime dateTime = DateTime.parse(date);
return DateFormat('yMMMD').format(dateTime);
} on FormatException {
return '';
} catch (e) {
return '';
}
}