NextTrain.fromJson constructor

NextTrain.fromJson(
  1. Map<String, dynamic> json
)

Creates a new NextTrain instance from a JSON object.

Implementation

factory NextTrain.fromJson(Map<String, dynamic> json) {
  final String? numCarsStr = json[ApiFields.car];
  final int? numCars = numCarsStr == null ? null : int.tryParse(numCarsStr);

  final String lineRaw = json[ApiFields.line] ?? '';
  final String line = lineRaw == 'No' ? '' : lineRaw;

  final String minutesAwayStr = json[ApiFields.min] ?? '';
  final int? minutesAway = int.tryParse(minutesAwayStr);

  late final TrainStatus? specialStatus;
  if (minutesAwayStr == "ARR" || minutesAwayStr == "BRD") {
    specialStatus = TrainStatus.fromString(minutesAwayStr);
  } else {
    specialStatus = null;
  }

  return NextTrain(
    locationCode: json[ApiFields.locationCode] ?? '',
    locationName: json[ApiFields.locationName] ?? '',
    numCars: numCars,
    destination: json[ApiFields.destination] ?? '',
    destinationCode: json[ApiFields.destinationCode],
    destinationName: json[ApiFields.destinationName],
    group: int.parse(json[ApiFields.group] ?? "-1"),
    line: line,
    minutesAway: minutesAway,
    specialStatus: specialStatus,
  );
}