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
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.