user3931627
user3931627

Reputation: 53

error when trying to decode json and casting it

I am trying to decode complex json data and always resulting in error. Below are my json data:

    {
    "status_code": 200,
    "message": "Successfully get data user",
    "data": {
        "sec_user": {
            "clientIp": "1.4.2.54",
            "officeid": "N/A",
        },
        "username": "uname",
        "name1": "",
        "menus": [
            {
                "appl_id": "MTC",
                "menu_caption": "Master",
                "menu_path": "/master",
                "submenus": [
                    {
                        "menu_caption": "Machine List",
                        "note1": "master",
                        "sub_menu_id": "1",
                        "route_path": "/machine_list"
                    },
                    {
                        "menu_caption": "Sparepart Category",
                        "note1": "master",
                        "sub_menu_id": "1",
                        "route_path": "/sparepart_category"
                    }
                ]
            },
            {
                "appl_id": "MTC",
                "menu_caption": "Master",
                "menu_path": "/master",
                "submenus": [
                    {
                        "menu_caption": "Machine List",
                        "note1": "master",
                        "sub_menu_id": "1",
                        "route_path": "/machine_list"
                    }
                ]
            },
        ]
    }
}

and I already create a model class using json to dart (and already retouch it a bit) and use json annotation and build runner. Then I get an error everytime I tried to decode the body of this json

if (jsonResponse.statusCode == 200) {
  final jsonItems =
      json.decode(jsonResponse.body).cast<Map<String, dynamic>>();
  List<Profile> profile = jsonItems.map<Profile>((json) {
    return MyMenu.fromJson(json);
  }).toList();
  return profile;
}

I always get this error message (attached on the image bellow): enter image description here

What when wrong with my code ?

Upvotes: 0

Views: 41

Answers (2)

user3931627
user3931627

Reputation: 53

End up answering my question, I did this:

json.decode(jsonResponse.body)['data']['menus'].cast<Map<String, dynamic>>();

Don't know if there is any other perfect method to do this.

Upvotes: 1

Mofidul Islam
Mofidul Islam

Reputation: 520

Your json had some syntax error as well.Please try this it if helps

import 'dart:convert';
    
    void main() {
      var jsonData= {
        "status_code": 200,
        "message": "Successfully get data user",
        "data": {
            "sec_user": {
                "clientIp": "1.4.2.54",
                "officeid": "N/A"
            },
            "username": "uname",
            "name1": "",
            "menus": [
                {
                    "appl_id": "MTC",
                    "menu_caption": "Master",
                    "menu_path": "/master",
                    "submenus": [
                        {
                            "menu_caption": "Machine List",
                            "note1": "master",
                            "sub_menu_id": "1",
                            "route_path": "/machine_list"
                        },
                        {
                            "menu_caption": "Sparepart Category",
                            "note1": "master",
                            "sub_menu_id": "1",
                            "route_path": "/sparepart_category"
                        }
                    ]
                },
                {
                    "appl_id": "MTC",
                    "menu_caption": "Master",
                    "menu_path": "/master",
                    "submenus": [
                        {
                            "menu_caption": "Machine List",
                            "note1": "master",
                            "sub_menu_id": "1",
                            "route_path": "/machine_list"
                        }
                    ]
                }
            ]
        }
    };
    final jsonItems =
          ApiResponse.fromJson(jsonData);
      print(jsonItems.message);
    }
    
    class ApiResponse {
      int statusCode;
      String message;
      Data data;
    
      ApiResponse({this.statusCode, this.message, this.data});
    
      ApiResponse.fromJson(Map<String, dynamic> json) {
        statusCode = json['status_code'];
        message = json['message'];
        data = json['data'] != null ? new Data.fromJson(json['data']) : null;
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['status_code'] = this.statusCode;
        data['message'] = this.message;
        if (this.data != null) {
          data['data'] = this.data.toJson();
        }
        return data;
      }
    }
    
    class Data {
      SecUser secUser;
      String username;
      String name1;
      List<Menus> menus;
    
      Data({this.secUser, this.username, this.name1, this.menus});
    
      Data.fromJson(Map<String, dynamic> json) {
        secUser = json['sec_user'] != null
            ? new SecUser.fromJson(json['sec_user'])
            : null;
        username = json['username'];
        name1 = json['name1'];
        if (json['menus'] != null) {
          menus = new List<Menus>();
          json['menus'].forEach((v) {
            menus.add(new Menus.fromJson(v));
          });
        }
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        if (this.secUser != null) {
          data['sec_user'] = this.secUser.toJson();
        }
        data['username'] = this.username;
        data['name1'] = this.name1;
        if (this.menus != null) {
          data['menus'] = this.menus.map((v) => v.toJson()).toList();
        }
        return data;
      }
    }
    
    class SecUser {
      String clientIp;
      String officeid;
    
      SecUser({this.clientIp, this.officeid});
    
      SecUser.fromJson(Map<String, dynamic> json) {
        clientIp = json['clientIp'];
        officeid = json['officeid'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['clientIp'] = this.clientIp;
        data['officeid'] = this.officeid;
        return data;
      }
    }
    
    class Menus {
      String applId;
      String menuCaption;
      String menuPath;
      List<Submenus> submenus;
    
      Menus({this.applId, this.menuCaption, this.menuPath, this.submenus});
    
      Menus.fromJson(Map<String, dynamic> json) {
        applId = json['appl_id'];
        menuCaption = json['menu_caption'];
        menuPath = json['menu_path'];
        if (json['submenus'] != null) {
          submenus = new List<Submenus>();
          json['submenus'].forEach((v) {
            submenus.add(new Submenus.fromJson(v));
          });
        }
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['appl_id'] = this.applId;
        data['menu_caption'] = this.menuCaption;
        data['menu_path'] = this.menuPath;
        if (this.submenus != null) {
          data['submenus'] = this.submenus.map((v) => v.toJson()).toList();
        }
        return data;
      }
    }
    
    class Submenus {
      String menuCaption;
      String note1;
      String subMenuId;
      String routePath;
    
      Submenus({this.menuCaption, this.note1, this.subMenuId, this.routePath});
    
      Submenus.fromJson(Map<String, dynamic> json) {
        menuCaption = json['menu_caption'];
        note1 = json['note1'];
        subMenuId = json['sub_menu_id'];
        routePath = json['route_path'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['menu_caption'] = this.menuCaption;
        data['note1'] = this.note1;
        data['sub_menu_id'] = this.subMenuId;
        data['route_path'] = this.routePath;
        return data;
      }
    }

Upvotes: 0

Related Questions