CurrentWeather.fromJson constructor

CurrentWeather.fromJson(
  1. Map<String, dynamic> json,
  2. UnitSettings settings
)

JSON deserialization constructor

Implementation

factory CurrentWeather.fromJson(
  Map<String, dynamic> json,
  UnitSettings settings,
) {
  // Mapping the iconId and ID to a WeatherType enum
  WeatherType weatherType = infoToWeatherType(
    id: json['weather'][0]['id'],
    iconId: json['weather'][0]['icon'],
  );

  // Formatting temperature
  num? temp = temperatureToSelectedUnit(
    temp: json['main']['temp'],
    unit: settings.temperatureUnit,
  );

  // Formatting feels like temperature
  num? feelsLike = temperatureToSelectedUnit(
    temp: json['main']['feels_like'],
    unit: settings.temperatureUnit,
  );

  // Formatting min temperature
  num? minTemp = temperatureToSelectedUnit(
    temp: json['main']['temp_min'],
    unit: settings.temperatureUnit,
  );

  // Formatting max temperature
  num? maxTemp = temperatureToSelectedUnit(
    temp: json['main']['temp_max'],
    unit: settings.temperatureUnit,
  );

  // Formatting the visibility
  num? visibility = distanceToSelectedUnit(
    distance: json['visibility'],
    unit: settings.distanceUnit,
  );
  // Formatting the pressure
  num pressure = pressureToSelectedUnit(
    pressure: json['main']['pressure'],
    unit: settings.pressureUnit,
  );

  // Formatting the pressure at ground level
  num pressureGroundLevel = pressureToSelectedUnit(
    pressure: json['main']['grnd_level'],
    unit: settings.pressureUnit,
  );

  // Formatting the pressure at sea level
  num pressureSeaLevel = pressureToSelectedUnit(
    pressure: json['main']['sea_level'],
    unit: settings.pressureUnit,
  );

  // Formatting the wind speed
  num? windSpeed = windSpeedToSelectedUnit(
    windSpeed: json['wind']['speed'],
    unit: settings.windSpeedUnit,
  );

  // Formatting the gust speed
  num? windGustSpeed = windSpeedToSelectedUnit(
    windSpeed: json['wind']['gust'],
    unit: settings.windSpeedUnit,
  );

  // Formatting the rain amount in the past 1 hour
  num? rainAmountLast1h = precipitationToSelectedUnit(
    amount: json['rain']['1h'],
    unit: settings.precipitationUnit,
  );

  // Formatting the rain amount in the past 3 hours
  num? rainAmountLast3h = precipitationToSelectedUnit(
    amount: json['rain']['3h'],
    unit: settings.precipitationUnit,
  );

  // Formatting the snow amount in the past 1 hour
  num? snowAmountLast1h = precipitationToSelectedUnit(
    amount: json['snow']['1h'],
    unit: settings.precipitationUnit,
  );

  // Formatting the snow amount in the past 3 hour
  num? snowAmountLast3h = precipitationToSelectedUnit(
    amount: json['snow']['3h'],
    unit: settings.precipitationUnit,
  );
  return CurrentWeather(
    cityName: json['name'],
    countryCode: json['sys']['country'],
    locationCoords: LocationCoords(
      longitude: json['coord']['lon'],
      latitude: json['coord']['lat'],
    ),
    weatherID: json['weather'][0]['id'],
    iconID: json['weather'][0]['icon'],
    weatherType: weatherType,
    mainDescription: json['weather'][0]['main'],
    secondaryDescription: json['weather'][0]['description'],
    temp: temp,
    feelsLike: feelsLike,
    minTemp: minTemp,
    maxTemp: maxTemp,
    visibility: visibility,
    humidity: json['main']['humidity'],
    cloudiness: json['clouds']['all'],
    pressure: pressure,
    pressureGroundLevel: pressureGroundLevel,
    pressureSeaLevel: pressureSeaLevel,
    windSpeed: windSpeed,
    windDegree: json['wind']['deg'],
    windGustSpeed: windGustSpeed,
    rainAmountLast1h: rainAmountLast1h,
    rainAmountLast3h: rainAmountLast3h,
    snowAmountLast1h: snowAmountLast1h,
    snowAmountLast3h: snowAmountLast3h,
    sunrise: DateTime.fromMillisecondsSinceEpoch(
      (json['sys']['sunrise']) * 1000,
      isUtc: true,
    ),
    sunset: DateTime.fromMillisecondsSinceEpoch(
      (json['sys']['sunset']) * 1000,
      isUtc: true,
    ),
    timeStamp: DateTime.fromMillisecondsSinceEpoch(
      (json['dt']) * 1000,
      isUtc: true,
    ),
    timezoneOffset: json['timezone'],
  );
}