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

32,376

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.

Share:
32,376
Muhammad Imanudin
Author by

Muhammad Imanudin

Updated on January 10, 2021

Comments

  • Muhammad Imanudin
    Muhammad Imanudin over 3 years

    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?

  • Muhammad Imanudin
    Muhammad Imanudin over 5 years
    Yess right.. I forgot to add a parameter in the URL. thanks
  • s.j
    s.j over 3 years
    @cbracken in my case all params passed and still getting this error all time