RailIncident.fromJson constructor

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

Creates a RailIncident object from a JSON object.

Implementation

factory RailIncident.fromJson(Map<String, dynamic> json) {
  late final List<String> affectedLines;

  late final bool affectedLinesWasString;

  if (json[ApiFields.linesAffected] == null) {
    affectedLines = const [];
    affectedLinesWasString = false;
  } else if (json[ApiFields.linesAffected] is String) {
    final String affectedLinesString = json[ApiFields.linesAffected] ?? '';

    affectedLines = affectedLinesString
        .split(RegExp(r';[\s]?'))
        .where((line) => line.isNotEmpty)
        .toList();

    affectedLinesWasString = true;
  } else if (json[ApiFields.linesAffected] is List) {
    affectedLines = List<String>.from(json[ApiFields.linesAffected] ?? []);

    affectedLinesWasString = false;
  } else {
    affectedLines = const [];
    affectedLinesWasString = false;
  }

  return RailIncident(
    timeOfLastUpdate:
        DateTime.tryParse(json[ApiFields.dateUpdated] ?? '') ?? emptyDateTime,
    description: json[ApiFields.description] ?? '',
    incidentId: json[ApiFields.incidentId] ?? '',
    incidentType: json[ApiFields.incidentType] ?? '',
    affectedLines: affectedLines,
  ).._affectedLinesWasString = affectedLinesWasString;
}