generateArrivalCards function Null safety

List<ArrivalCard> generateArrivalCards(
  1. List<VehicleEstimation> vehicleEstimations,
  2. Stop stop,
  3. dynamic routes
)

Implementation

List<ArrivalCard> generateArrivalCards(
    List<VehicleEstimation> vehicleEstimations, Stop stop, dynamic routes) {
  const int nEstimations = 3;
  List<ArrivalCard> arrivalCards = [];
  Map<Route, List<DateTime>> cardContent = {};

  for (VehicleEstimation vehicleEstimation in vehicleEstimations) {
    var key = cardContent.keys.firstWhere(
        (element) => element.id == vehicleEstimation.vehicle.routeWay.routeId,
        orElse: () =>
            Route.fromJson(routes[vehicleEstimation.vehicle.routeWay.routeId]));
    cardContent.update(
        key, (value) => [...value, vehicleEstimation.estimation.estimation],
        ifAbsent: () => [vehicleEstimation.estimation.estimation]);
  }

  // The following code would add routes that don't have estimations
  // at the moment. It's commented out because `stop` contains routes
  // that it shouldn't.
  /*for (RouteWay routeWay in stop.routeWays) {
    if (cardContent.keys
            .firstWhereOrNull((element) => element.id == routeWay.routeId) ==
        null) {
      cardContent.putIfAbsent(
          Route.fromJson(routes[routeWay.routeId]), () => []);
    }
  }*/

  cardContent.forEach((key, value) {
    arrivalCards.add(ArrivalCard(
      route: key.code,
      estimations: value.take(nEstimations).toList(),
      icon: getIcon(key.transportType),
      iconColor: key.color,
    ));
  });

  return arrivalCards;
}