fetchNextTrains static method
Implementation
static Future<List<NextTrain>> fetchNextTrains(
String apiKey, {
required String stationCodes,
}) async {
final String host = _buildUrl(
stationCodes: stationCodes,
);
final response = await http.get(
Uri.parse(host),
headers: {"api_key": apiKey},
);
Map<String, dynamic> responseArr = readJson(response.body);
if (responseArr.isEmpty) return [];
if (responseArr[ApiFields.trains] == null) return [];
if (responseArr[ApiFields.trains] is List) {
return ((responseArr[ApiFields.trains] as List?) ?? [])
.map((nextTrain) => NextTrain.fromJson(nextTrain))
.toList();
}
return [];
}