69 lines
No EOL
1.9 KiB
Dart
69 lines
No EOL
1.9 KiB
Dart
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
String proxyURL = "https://laserscouter.halfheart.net/";
|
|
|
|
class TeamSearchResult {
|
|
final List<String> eventNames;
|
|
final List<String> eventCodes;
|
|
|
|
TeamSearchResult({required this.eventNames, required this.eventCodes});
|
|
}
|
|
|
|
class EventSearchResult {
|
|
final List<String> teamNames;
|
|
final List<String> teamCodes;
|
|
|
|
EventSearchResult({required this.teamNames, required this.teamCodes});
|
|
}
|
|
|
|
Future<TeamSearchResult> teamSearch(String teamNumber) async {
|
|
try {
|
|
final response = await http.get(
|
|
Uri.parse('${proxyURL}teamsearch/$teamNumber'),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
List<String> eventNames = [];
|
|
List<String> eventCodes = [];
|
|
|
|
for (var event in data) {
|
|
eventNames.add(event['name']);
|
|
eventCodes.add(event['key']);
|
|
}
|
|
return TeamSearchResult(eventNames: eventNames, eventCodes: eventCodes);
|
|
} else {
|
|
throw Exception('Failed to load events. Status code: ${response.statusCode}');
|
|
}
|
|
} catch (e) {
|
|
debugPrint('Error in teamSearch: $e');
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<EventSearchResult> eventSearch(String eventCode) async {
|
|
try {
|
|
final response = await http.get(
|
|
Uri.parse('${proxyURL}eventsearch/$eventCode'),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
List<String> teamNames = [];
|
|
List<String> teamCodes = [];
|
|
|
|
for (var event in data) {
|
|
teamNames.add(event['nickname']);
|
|
teamCodes.add(event['team_number'].toString());
|
|
}
|
|
return EventSearchResult(teamNames: teamNames, teamCodes: teamCodes);
|
|
} else {
|
|
throw Exception('Failed to load teams. Status code: ${response.statusCode}');
|
|
}
|
|
} catch (e) {
|
|
debugPrint('Error in eventSearch: $e');
|
|
rethrow;
|
|
}
|
|
} |