Handling Data Time parse Error in flutter

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

i’m using https://pub.dev/packages/intl

enter image description here

Output when the data isNot = ”.

enter image description here

>Solution :

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

Leave a Reply