getForecastWeather method
Fetch forecast weather data (next N days).
Returns HourlyWeatherData with hourly forecast data for the next days.
Maximum days is 16 (Open-Meteo forecast limit).
Implementation
Future<HourlyWeatherData> getForecastWeather({
required double latitude,
required double longitude,
int days = 7,
}) async {
if (days < 1 || days > 16) {
throw ArgumentError('days must be between 1 and 16');
}
final uri = Uri.parse(_forecastUrl).replace(
queryParameters: {
'latitude': latitude.toString(),
'longitude': longitude.toString(),
'forecast_days': days.toString(),
'hourly': [
'temperature_2m',
'relative_humidity_2m',
'wind_speed_10m',
'precipitation',
].join(','),
'timezone': 'Australia/Sydney',
},
);
try {
final response = await http.get(uri);
if (response.statusCode == 200) {
final json = jsonDecode(response.body) as Map<String, dynamic>;
return HourlyWeatherData.fromJson(json);
} else {
throw Exception(
'Failed to load forecast weather data: ${response.statusCode}',
);
}
} catch (e) {
throw Exception('Failed to fetch forecast weather: $e');
}
}