fetchBusIncidents static method

Future<List<BusIncident>> fetchBusIncidents(
  1. String apiKey, {
  2. String? routeId,
})

Implementation

static Future<List<BusIncident>> fetchBusIncidents(
  String apiKey, {
  String? routeId,
}) async {
  final String host = _buildUrl(
    routeId: routeId,
  );

  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.busIncidents] == null) return [];

  if (responseArr[ApiFields.busIncidents] is List) {
    return ((responseArr[ApiFields.busIncidents] as List?) ?? [])
        .map((busIncident) => BusIncident.fromJson(busIncident))
        .toList();
  }

  return [];
}