Mapping a list inside another in flutter

Viewed 58

I'm trying to map a list which has another list of objects but an error keep showing . Thank you for any help in advance

  • My Class
 import 'Standing.dart';

class League {
    String? country;
    String? flag;
    int? id;
    String? logo;
    String? name;
    int? season;
    List<List<Standing>>? standings;

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

    factory League.fromJson(Map<String, dynamic> json) {
        return League(
            country: json['country'], 
            flag: json['flag'], 
            id: json['id'], 
            logo: json['logo'], 
            name: json['name'], 
            season: json['season'],
            standings: json['standings'] != null ? (json['standings'] as List).map((i) => List<Standing>.from(i)).toList() : [],
        );
    }

    Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = <String, dynamic>{};
        data['country'] = country;
        data['flag'] = flag;
        data['id'] = id;
        data['logo'] = logo;
        data['name'] = name;
        data['season'] = season;
        data['standings'] = standings?.map((v) => List<Standing>.from(v)).toList();
        return data;
    }
}
  • Standings Class
 import 'All.dart';
import 'Away.dart';
import 'Home.dart';
import 'Team.dart';

class Standing {
    All? all;
    Away? away;
    String? form;
    int? goalsDiff;
    String? group;
    Home? home;
    int? points;
    int? rank;
    String? status;
    Team? team;
    String? update;

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

    factory Standing.fromJson(Map<String, dynamic> json) {
        return Standing(
            all: json['all'] != null ? All.fromJson(json['all']) : null, 
            away: json['away'] != null ? Away.fromJson(json['away']) : null,
            form: json['form'], 
            goalsDiff: json['goalsDiff'], 
            group: json['group'], 
            home: json['home'] != null ? Home.fromJson(json['home']) : null, 
            points: json['points'], 
            rank: json['rank'], 
            status: json['status'], 
            team: json['team'] != null ? Team.fromJson(json['team']) : null, 
            update: json['update'], 
        );
    }

    Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['form'] = this.form;
        data['goalsDiff'] = this.goalsDiff;
        data['group'] = this.group;
        data['points'] = this.points;
        data['rank'] = this.rank;
        data['status'] = this.status;
        data['update'] = this.update;
        final all = this.all;
        if (all != null) {
            data['all'] = all.toJson();
        }
        final away = this.away;
        if (away != null) {
            data['away'] = away.toJson();
        }

        final home = this.home;
        if (home != null) {
            data['home'] = home.toJson();
        }
        final team = this.team;
        if (team != null) {
            data['team'] = team.toJson();
        }
        return data;
    }
}

  • data parsing
 Future<StModel?> getStandings(int leagueId , int season) async {
    HttpWithMiddleware httpClient = HttpWithMiddleware.build(middlewares: [
      HttpLogger(logLevel: LogLevel.BODY),
    ]);

    var fullUrl  = "${Utils.BASE_URL}standings?league=$leagueId&season=$season";
    var response = await httpClient.get(Uri.parse(fullUrl),headers: {
      'x-rapidapi-host' : Utils.HOST,
      'x-rapidapi-key' :  Utils.KEY,
    });

    if(response.statusCode == 200){
      var json  = jsonDecode(response.body);
      return StModel.fromJson(json);
    } else {
      return null;
    }
  }

  • Error enter image description here
1 Answers

Change this:

factory League.fromJson(Map<String, dynamic> json) {
    return League(
        country: json['country'], 
        flag: json['flag'], 
        id: json['id'], 
        logo: json['logo'], 
        name: json['name'], 
        season: json['season'],
        standings: json['standings'] != null ? (json['standings'] as List).map((i) => List<Standing>.from(i)).toList() : [],
    );
}

to:

    factory League.fromJson(Map<String, dynamic> json) {
    List<List<Standing>> _standings = [];
    for (var element in json['league']['standings'] as List) {
      List<Standing> temp = [];
      for (var item in element) {
        temp.add(Standing.fromJson(item));
      }
      _standings.add(temp);
    }
    return League(
      country: json['country'],
      flag: json['flag'],
      id: json['id'],
      logo: json['logo'],
      name: json['name'],
      season: json['season'],
      standings: _standings,
    );
  }

then use it like this:

if(response.statusCode == 200){
  var json  = jsonDecode(response.body);
  return League.fromJson(json['response']);
} else {
  return null;
}
Related