Home > Back-end >  Flutter Map or .fromJson of my not working
Flutter Map or .fromJson of my not working

Time:05-06

I have data response with api and i can't map it to List but it return users null, i think is not working because method fromJson

this is data return with api This is data return with API

and this is my models Models and method fromJson

My controller enter image description here

end is Debug window enter image description here

Thanks for any answer

CodePudding user response:

Could you further specify what it is that you get as a result? Is the whole list full of null? Or are some of the objects' fields null?

Here is what I believe is an error:

nameUser: json['tenNguoiDung'] == null ? '' : json['ten']
avatarUser: json['nguoiDung_anhDinhKem'] == null ? '' : json['avatar'],

You are saying:

If tenNguoiDung is null: nameUser is equal to '', if it isn't, nameUser = json['ten'].

But your json doesn't have a 'ten' field, so it will be null. What you are most likely looking for is the if-null operator:

nameUser: json['tenNguoiDung'] ?? ''
avatarUser: json['nguoiDung_anhDinhKem'] ?? ''

which will assign '' if the json field is null.

  • Related