Home > Mobile >  error in Flutter : Unhandled Exception: type 'Null' is not a subtype of type 'String&
error in Flutter : Unhandled Exception: type 'Null' is not a subtype of type 'String&

Time:12-16

i have this error when i call the data from api the problem based in DateTime attribut,

error : Unhandled Exception: type 'Null' is not a subtype of type 'String'

required this.dateTime,

DateTime dateTime;

DateTime.parse(json["dateTime"]),

"dateTime": dateTime.toIso8601String(),

CodePudding user response:

Use This

"dateTime": dateTime.toString()

CodePudding user response:

Its Null safety issue when you are accessing: json["dateTime"] this value and parsing into DateTime like:-DateTime.parse(json["dateTime"] and if this value json["dateTime"] will be null then this issue will be occure. by changing little bit in your code you'll re-solve this issue.

required this.dateTime,

DateTime dateTime;

DateTime.parse(json["dateTime"].toString()),// this line you need to edit. 

"dateTime": dateTime.toIso8601String(),

Now if json["dateTime"] this value will be null then no any issue will be occure because null value will be convert into string. by adding .toString().

  • Related