enum CardType { red, yellow, green } enum Team { home, away } enum GoalType { fieldGoal, shortCorner, penaltyStroke } class Card { final String datetime; final int player; final CardType cardType; final int seconds; final Team team; final int duration; Card({ required this.datetime, required this.player, required this.cardType, required this.seconds, required this.team, required this.duration, }); } class Goal { final String datetime; final int player; final GoalType goalType; final int seconds; final Team team; Goal({ required this.datetime, required this.player, required this.goalType, required this.seconds, required this.team, }); } class Match { Match({ required this.datetime, required this.homeScore, required this.awayScore, required this.goals, required this.cards, }); final String datetime; final int homeScore; final int awayScore; final List goals; final List cards; Map toJson() { return { 'datetime': datetime, 'homeScore': homeScore, 'awayScore': awayScore, 'goals': goals.map((goal) => { 'datetime': goal.datetime, 'player': goal.player, 'goal_type': goal.goalType.index, 'elapsed': goal.seconds, 'team': goal.team.index, }).toList(), 'cards': cards.map((card) => { 'datetime': card.datetime, 'player': card.player, 'card_type': card.cardType.index, 'elapsed': card.seconds, 'team': card.team.index, 'duration': card.duration, }).toList(), }; } factory Match.fromJson(Map json) { return Match( datetime: json['datetime'] as String, homeScore: json['homeScore'] as int, awayScore: json['awayScore'] as int, goals: ((json['goals'] as List?) ?? []).map((goalJson) { final goal = goalJson as Map; return Goal( datetime: goal['datetime'] as String, player: goal['player'] as int, goalType: _enumValue( GoalType.values, goal['goal_type'] ?? goal['goalType'], GoalType.fieldGoal, ), seconds: (goal['elapsed'] ?? goal['seconds'] ?? 0) as int, team: _enumValue(Team.values, goal['team'], Team.home), ); }).toList(), cards: ((json['cards'] as List?) ?? []).map((cardJson) { final card = cardJson as Map; return Card( datetime: card['datetime'] as String, player: card['player'] as int, cardType: _enumValue( CardType.values, card['card_type'] ?? card['cardType'], CardType.red, ), seconds: (card['elapsed'] ?? card['seconds'] ?? 0) as int, team: _enumValue(Team.values, card['team'], Team.home), duration: card['duration'] as int? ?? 0, ); }).toList(), ); } static T _enumValue(List values, dynamic value, T fallback) { if (value is int && value >= 0 && value < values.length) { return values[value]; } if (value is String) { for (final item in values) { if (item.name == value.split('.').last) return item; } } return fallback; } }