Home > Back-end >  Flutter how to check for null values in api response Json when parsing it to dart class, below is my
Flutter how to check for null values in api response Json when parsing it to dart class, below is my

Time:10-24

for the calls where the 'Building object' is null is get this error below,

datatype 'Null' is not a subtype of type 'Map<String, dynamic>'

but for the call where all the data is available, I don't get any error, so how can I make my dart class check for null values without getting the error above, below is my dart class modal.

enter image description here

CodePudding user response:

In model you can define values like paidByNum = json['paidByNum'] ?? defaultValue;

For example if paidByNum is of type int then paidByNum = json['paidByNum'] ?? 0;

For string paidByNum = json['paidByNum'] ?? "";

This will replace the null value with default value.

CodePudding user response:

simply do a null check and assign a default or null

_data['id'] = id == null ? null : id;

PS: typed code is more welcome than screenshots

  • Related