Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading