@@ -21,20 +21,21 @@ class Match {
2121
2222 factory Match .fromJson (Map <String , dynamic > json) {
2323 return Match (
24- id: json['#' ] as String ,
25- fieldRaw: json['Field' ] as String ,
26- team1: json['Teams_1' ] as String ,
27- team2: json['Teams_3' ] as String ,
28-
29- // Extract only the number from the fieldRaw string
30- // Example: "L-1" -> "1"
31- field: RegExp (r'\d+' ).firstMatch (json['Field' ] as String )? .group (0 ) ?? '' ,
24+ id: json['number' ]? .toString () ?? '' , // Safely extract match number as the ID
25+ fieldRaw: json['pitch' ] as String ? ?? '' , // Safely extract pitch
26+ team1: json['team1' ]? ['name' ] as String ? ?? 'Unknown Team 1' , // Safely extract team1 name
27+ team2: json['team2' ]? ['name' ] as String ? ?? 'Unknown Team 2' , // Safely extract team2 name
28+ field: RegExp (r'\d+' )
29+ .firstMatch (json['pitch' ] as String ? ?? '' )
30+ ? .group (0 )
31+ ? .replaceFirst (RegExp (r'^0+' ), '' ) ??
32+ '' , // Safely extract field number
3233 );
3334 }
3435}
3536
3637class MatchDataService {
37- String _url = 'https://raw.githubusercontent. com/robocup-junior/soccer-matches/refs/heads/main/data/ RCJI25/matches. json' ;
38+ String _url = 'https://catigoal. com/rest/v1/ RCJI25/matches?format= json' ;
3839 String _matchId = '' ;
3940 String _state = '' ;
4041 List <Match > _matches = [];
@@ -50,28 +51,34 @@ class MatchDataService {
5051 /// Loads MQTT settings from SharedPreferences
5152 Future <void > loadPreferences () async {
5253 prefs = await SharedPreferences .getInstance ();
53- _url = prefs.getString ('matches_url' ) ?? 'https://raw.githubusercontent. com/robocup-junior/soccer-matches/refs/heads/main/data/ RCJI25/matches. json' ;
54+ _url = prefs.getString ('matches_url' ) ?? 'https://catigoal. com/rest/v1/ RCJI25/matches?format= json' ;
5455 }
5556
5657 Future <List <Match >> fetchMatches (String url) async {
5758 final response = await http.get (Uri .parse (url));
5859
59- //print(response.body);
6060 if (response.statusCode == 200 ) {
6161 print ('Matches loaded successfully' );
62- final List <dynamic > jsonList = json.decode (response.body);
63- print ('Number of matches: ${jsonList .length }' );
64- return jsonList.map ((json) => Match .fromJson (json)).toList ();
62+ final Map <String , dynamic > jsonResponse = json.decode (response.body);
63+ final List <dynamic > matchesList = jsonResponse['matches' ];
64+ print ('Number of matches: ${matchesList .length }' );
65+ return matchesList.map ((matchJson) => Match .fromJson (matchJson)).toList ();
6566 } else {
6667 throw Exception ('Failed to load matches' );
6768 }
6869 }
6970
7071 Match ? findMatchById (List <Match > matches, String gameId) {
7172 try {
72- return matches.firstWhere ((match) => match.id == gameId);
73+ // Iterate through matches and find the one with the matching ID
74+ for (var match in matches) {
75+ if (match.id == gameId) {
76+ return match;
77+ }
78+ }
79+ return null ; // Return null if no match is found
7380 } catch (e) {
74- return null ;
81+ return null ; // Handle errors gracefully
7582 }
7683 }
7784
0 commit comments