Home > Software engineering >  Handling Data Time parse Error in flutter
Handling Data Time parse Error in flutter

Time:05-03

How to handle this error if my API output is "datePosted": ""

i'm using enter image description here

Output when the data isNot = ''.

enter image description here

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 '';
  }
}

  • Related