Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter double nested json list to Dart model

I’m trying to convert this json response to a dart model, but I can’t figure out how to parse "standings" which is a double nested list. What im trying at the moment doesnt work (i thought i should access the first element in the higher list):

 StandingLeague.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    country = json['country'];
    logo = json['logo'];
    flag = json['flag'];
    season = json['season'];
    if (json['standings'] != null) {
      standings = <Standing>[];
      json['standings'][0].forEach((v) { standings?.add(v); });
    }
  }

Json response:

{
        "league": {
            "id": 39,
            "name": "Premier League",
            "country": "England",
            "logo": "https://media.api-sports.io/football/leagues/39.png",
            "flag": "https://media.api-sports.io/flags/gb.svg",
            "season": 2022,
            "standings": [
                [
                    {
                        "rank": 1,
                        "team": {
                            "id": 42,
                            "name": "Arsenal",
                            "logo": "https://media.api-sports.io/football/teams/42.png"
                        },
                        "points": 18,
                        "goalsDiff": 10,
                        "group": "Premier League",
                        "form": "WLWWW",
                        "status": "same",
                        "description": "Promotion - Champions League (Group Stage)",
                        "all": {
                            "played": 7,
                            "win": 6,
                            "draw": 0,
                            "lose": 1,
                            "goals": {
                                "for": 17,
                                "against": 7
                            }
                        },
                        "home": {
                            "played": 3,
                            "win": 3,
                            "draw": 0,
                            "lose": 0,
                            "goals": {
                                "for": 8,
                                "against": 4
                            }
                        },
                        "away": {
                            "played": 4,
                            "win": 3,
                            "draw": 0,
                            "lose": 1,
                            "goals": {
                                "for": 9,
                                "against": 3
                            }
                        },
                        "update": "2022-09-18T00:00:00+00:00"
                    },
                    {
                        "rank": 2,
                        "team": {
                            "id": 50,
                            "name": "Manchester City",
                            "logo": "https://media.api-sports.io/football/teams/50.png"
                        },
                        "points": 17,
                        "goalsDiff": 17,
                        "group": "Premier League",
                        "form": "WDWWD",
                        "status": "same",
                        "description": "Promotion - Champions League (Group Stage)",
                        "all": {
                            "played": 7,
                            "win": 5,
                            "draw": 2,
                            "lose": 0,
                            "goals": {
                                "for": 23,
                                "against": 6
                            }
                        },
                        "home": {
                            "played": 3,
                            "win": 3,
                            "draw": 0,
                            "lose": 0,
                            "goals": {
                                "for": 14,
                                "against": 2
                            }
                        },
                        "away": {
                            "played": 4,
                            "win": 2,
                            "draw": 2,
                            "lose": 0,
                            "goals": {
                                "for": 9,
                                "against": 4
                            }
                        },
                        "update": "2022-09-18T00:00:00+00:00"
                    },
                    {
                        "rank": 3,
                        "team": {
                            "id": 47,
                            "name": "Tottenham",
                            "logo": "https://media.api-sports.io/football/teams/47.png"
                        },
                        "points": 17,
                        "goalsDiff": 11,
                        "group": "Premier League",
                        "form": "WWDWW",
                        "status": "same",
                        "description": "Promotion - Champions League (Group Stage)",
                        "all": {
                            "played": 7,
                            "win": 5,
                            "draw": 2,
                            "lose": 0,
                            "goals": {
                                "for": 18,
                                "against": 7
                            }
                        },
                        "home": {
                            "played": 4,
                            "win": 4,
                            "draw": 0,
                            "lose": 0,
                            "goals": {
                                "for": 13,
                                "against": 4
                            }
                        },
                        "away": {
                            "played": 3,
                            "win": 1,
                            "draw": 2,
                            "lose": 0,
                            "goals": {
                                "for": 5,
                                "against": 3
                            }
                        },
                        "update": "2022-09-18T00:00:00+00:00"
                    },
                    {
                        "rank": 4,
                        "team": {
                            "id": 51,
                            "name": "Brighton",
                            "logo": "https://media.api-sports.io/football/teams/51.png"
                        },
                        "points": 13,
                        "goalsDiff": 6,
                        "group": "Premier League",
                        "form": "WLWWD",
                        "status": "same",
                        "description": "Promotion - Champions League (Group Stage)",
                        "all": {
                            "played": 6,
                            "win": 4,
                            "draw": 1,
                            "lose": 1,
                            "goals": {
                                "for": 11,
                                "against": 5
                            }
                        },
                        "home": {
                            "played": 3,
                            "win": 2,
                            "draw": 1,
                            "lose": 0,
                            "goals": {
                                "for": 6,
                                "against": 2
                            }
                        },
                        "away": {
                            "played": 3,
                            "win": 2,
                            "draw": 0,
                            "lose": 1,
                            "goals": {
                                "for": 5,
                                "against": 3
                            }
                        },
                        "update": "2022-09-18T00:00:00+00:00"
                    },
                    {
                        "rank": 5,
                        "team": {
                            "id": 33,
                            "name": "Manchester United",
                            "logo": "https://media.api-sports.io/football/teams/33.png"
                        },
                        "points": 12,
                        "goalsDiff": 0,
                        "group": "Premier League",
                        "form": "WWWWL",
                        "status": "same",
                        "description": "Promotion - Europa League (Group Stage)",
                        "all": {
                            "played": 6,
                            "win": 4,
                            "draw": 0,
                            "lose": 2,
                            "goals": {
                                "for": 8,
                                "against": 8
                            }
                        },
                        "home": {
                            "played": 3,
                            "win": 2,
                            "draw": 0,
                            "lose": 1,
                            "goals": {
                                "for": 6,
                                "against": 4
                            }
                        },
                        "away": {
                            "played": 3,
                            "win": 2,
                            "draw": 0,
                            "lose": 1,
                            "goals": {
                                "for": 2,
                                "against": 4
                            }
                        },
                        "update": "2022-09-18T00:00:00+00:00"
                    },
                    {
                        "rank": 6,
                        "team": {
                            "id": 36,
                            "name": "Fulham",
                            "logo": "https://media.api-sports.io/football/teams/36.png"
                        },
                        "points": 11,
                        "goalsDiff": 1,
                        "group": "Premier League",
                        "form": "WLWLW",
                        "status": "same",
                        "description": null,
                        "all": {
                            "played": 7,
                            "win": 3,
                            "draw": 2,
                            "lose": 2,
                            "goals": {
                                "for": 12,
                                "against": 11
                            }
                        },
                        "home": {
                            "played": 3,
                            "win": 2,
                            "draw": 1,
                            "lose": 0,
                            "goals": {
                                "for": 7,
                                "against": 5
                            }
                        },
                        "away": {
                            "played": 4,
                            "win": 1,
                            "draw": 1,
                            "lose": 2,
                            "goals": {
                                "for": 5,
                                "against": 6
                            }
                        },
                        "update": "2022-09-18T00:00:00+00:00"
                    },
                    {
                        "rank": 7,
                        "team": {
                            "id": 49,
                            "name": "Chelsea",
                            "logo": "https://media.api-sports.io/football/teams/49.png"
                        },
                        "points": 10,
                        "goalsDiff": -1,
                        "group": "Premier League",
                        "form": "WLWLD",
                        "status": "same",
                        "description": null,
                        "all": {
                            "played": 6,
                            "win": 3,
                            "draw": 1,
                            "lose": 2,
                            "goals": {
                                "for": 8,
                                "against": 9
                            }
                        },
                        "home": {
                            "played": 3,
                            "win": 2,
                            "draw": 1,
                            "lose": 0,
                            "goals": {
                                "for": 6,
                                "against": 4
                            }
                        },
                        "away": {
                            "played": 3,
                            "win": 1,
                            "draw": 0,
                            "lose": 2,
                            "goals": {
                                "for": 2,
                                "against": 5
                            }
                        },
                        "update": "2022-09-18T00:00:00+00:00"
                    }
                ]
            ]
        }
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can give instance of your json to https://app.quicktype.io/ to get model based on, here is example of your json model:

// To parse this JSON data, do
//
//     final standingLeague = standingLeagueFromMap(jsonString);

import 'package:meta/meta.dart';
import 'dart:convert';

StandingLeague standingLeagueFromMap(String str) => StandingLeague.fromMap(json.decode(str));

String standingLeagueToMap(StandingLeague data) => json.encode(data.toMap());

class StandingLeague {
    StandingLeague({
        @required this.league,
    });

    League league;

    factory StandingLeague.fromMap(Map<String, dynamic> json) => StandingLeague(
        league: League.fromMap(json["league"]),
    );

    Map<String, dynamic> toMap() => {
        "league": league.toMap(),
    };
}

class League {
    League({
        @required this.id,
        @required this.name,
        @required this.country,
        @required this.logo,
        @required this.flag,
        @required this.season,
        @required this.standings,
    });

    int id;
    String name;
    String country;
    String logo;
    String flag;
    int season;
    List<List<Standing>> standings;

    factory League.fromMap(Map<String, dynamic> json) => League(
        id: json["id"],
        name: json["name"],
        country: json["country"],
        logo: json["logo"],
        flag: json["flag"],
        season: json["season"],
        standings: List<List<Standing>>.from(json["standings"].map((x) => List<Standing>.from(x.map((x) => Standing.fromMap(x))))),
    );

    Map<String, dynamic> toMap() => {
        "id": id,
        "name": name,
        "country": country,
        "logo": logo,
        "flag": flag,
        "season": season,
        "standings": List<dynamic>.from(standings.map((x) => List<dynamic>.from(x.map((x) => x.toMap())))),
    };
}

class Standing {
    Standing({
        @required this.rank,
        @required this.team,
        @required this.points,
        @required this.goalsDiff,
        @required this.group,
        @required this.form,
        @required this.status,
        @required this.description,
        @required this.all,
        @required this.home,
        @required this.away,
        @required this.update,
    });

    int rank;
    Team team;
    int points;
    int goalsDiff;
    String group;
    String form;
    String status;
    String description;
    All all;
    All home;
    All away;
    DateTime update;

    factory Standing.fromMap(Map<String, dynamic> json) => Standing(
        rank: json["rank"],
        team: Team.fromMap(json["team"]),
        points: json["points"],
        goalsDiff: json["goalsDiff"],
        group: json["group"],
        form: json["form"],
        status: json["status"],
        description: json["description"] == null ? null : json["description"],
        all: All.fromMap(json["all"]),
        home: All.fromMap(json["home"]),
        away: All.fromMap(json["away"]),
        update: DateTime.parse(json["update"]),
    );

    Map<String, dynamic> toMap() => {
        "rank": rank,
        "team": team.toMap(),
        "points": points,
        "goalsDiff": goalsDiff,
        "group": group,
        "form": form,
        "status": status,
        "description": description == null ? null : description,
        "all": all.toMap(),
        "home": home.toMap(),
        "away": away.toMap(),
        "update": update.toIso8601String(),
    };
}

class All {
    All({
        @required this.played,
        @required this.win,
        @required this.draw,
        @required this.lose,
        @required this.goals,
    });

    int played;
    int win;
    int draw;
    int lose;
    Goals goals;

    factory All.fromMap(Map<String, dynamic> json) => All(
        played: json["played"],
        win: json["win"],
        draw: json["draw"],
        lose: json["lose"],
        goals: Goals.fromMap(json["goals"]),
    );

    Map<String, dynamic> toMap() => {
        "played": played,
        "win": win,
        "draw": draw,
        "lose": lose,
        "goals": goals.toMap(),
    };
}

class Goals {
    Goals({
        @required this.goalsFor,
        @required this.against,
    });

    int goalsFor;
    int against;

    factory Goals.fromMap(Map<String, dynamic> json) => Goals(
        goalsFor: json["for"],
        against: json["against"],
    );

    Map<String, dynamic> toMap() => {
        "for": goalsFor,
        "against": against,
    };
}

class Team {
    Team({
        @required this.id,
        @required this.name,
        @required this.logo,
    });

    int id;
    String name;
    String logo;

    factory Team.fromMap(Map<String, dynamic> json) => Team(
        id: json["id"],
        name: json["name"],
        logo: json["logo"],
    );

    Map<String, dynamic> toMap() => {
        "id": id,
        "name": name,
        "logo": logo,
    };
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading