Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

android - flutter - The method '[]' was called on null (parse json)

how do make a 'Model' with a Json structure like this? see image

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...