getCurrentWeather method

Future<WeatherData> getCurrentWeather({
  1. required double latitude,
  2. required double longitude,
})

Fetch current weather data for a location.

Returns WeatherData for the specified latitude and longitude. Throws an exception if the request fails.

Implementation

Future<WeatherData> getCurrentWeather({
  required double latitude,
  required double longitude,
}) async {
  final uri = Uri.parse(_forecastUrl).replace(
    queryParameters: {
      'latitude': latitude.toString(),
      'longitude': longitude.toString(),
      'current': [
        'temperature_2m',
        'relative_humidity_2m',
        'precipitation',
        'weather_code',
        'wind_speed_10m',
        'wind_direction_10m',
      ].join(','),
      'hourly': ['precipitation'].join(','),
      'daily': ['temperature_2m_max', 'temperature_2m_min'].join(','),
      'timezone': 'Australia/Sydney',
      'forecast_days': '1',
    },
  );

  try {
    final response = await http.get(uri);

    if (response.statusCode == 200) {
      final json = jsonDecode(response.body) as Map<String, dynamic>;
      return WeatherData.fromJson(json);
    } else {
      throw Exception('Failed to load weather data: ${response.statusCode}');
    }
  } catch (e) {
    throw Exception('Failed to fetch weather: $e');
  }
}