how do make a 'Model' with a Json structure like this?
-- beforely, I tried parsing json in another form and it worked, and when I tried to make the structure for json above like this:
class HomeTeam {
final int id, legacyId, name, countryId;
final bool nationalTeam;
final String logoPath;
HomeTeam({this.id, this.legacyId, this.name, this.countryId, this.nationalTeam, this.logoPath});
factory HomeTeam.fromJson(Map<String, dynamic> json){
return HomeTeam(
id: json['id'],
legacyId: json['legacy_id'],
name: json['name'],
nationalTeam: json['national_team'],
logoPath: json['logo_path']
);
}
}
.
homeTeam: HomeTeam.fromJson(json['localTeam']['data']),
Then the result appears an error..
NoSuchMethodError: The method '[]' was called on null. E/flutter ( 220): Receiver: null E/flutter: Tried calling:
how to solve this problem?
The error message NoSuchMethodError: The method '[]' was called on null.
is telling you that you've called the index ([]
) operator on null.
Without a reduced code sample and JSON data in your question, or a stack trace that points to a particular line in your code, it's hard to tell which value is null. You'll want to examine the stack trace and look at the line number where the failure occurred.
For example, let's imagine you see:
Unhandled exception:
NoSuchMethodError: The method '[]' was called on null.
Receiver: null
Tried calling: []("data")
#0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
#1 main (file:///Users/cbracken/foo.dart:3:23)
...
The stack trace above is telling you that the call on the null object was in main
on line 3 of file foo.dart. Further, it's telling you that the []
operator was called with the parameter 'data'
. If I look at that line in my code and it says var foo = json['localteam']['data']
, then I would deduce that json['localteam']
is returning null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With