Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing DateTime from Json error in Flutter

Im trying to get simple DateTime format from Json to my flutter app and however i try i still get this type of format : 2019-03-28 10:06:27.090Z

And im getting this error:

E/flutter (27849): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: ApiException 500: Exception during deserialization. (Inner exception: {innerException})
E/flutter (27849): 
E/flutter (27849): #0      DateTime.parse (dart:core/date_time.dart:335:7)*

Here is code example:

  MyClass.fromJson(Map<String, dynamic> json) {
    if (json == null) return;
    name = json['name'];
    deletionTime = json['deletionTime'] == null ? null : DateTime.parse(json['deletionTime']);
    lastModificationTime = json['lastModificationTime'] == null ? null : DateTime.parse(json['lastModificationTime']);
    creationTime = json['creationTime'] == null ? null : DateTime.parse(json['creationTime']);
    id = json['id'];
  }

Im expecting Json to successfully parse to DateTime.

like image 707
lbracic Avatar asked Jul 13 '26 00:07

lbracic


1 Answers

DateTime.parse only accepts String arguments. Since value is dynamic check type using json['key'].runtimeType and try following solution.

DateTime.parse(json['deletionTime'].toString());

DateTime.parse(json['creationTime'].toString());
like image 81
Tirth Patel Avatar answered Jul 14 '26 14:07

Tirth Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!