getHourlyForecast method
//////////// //////////// Retrieves weather forecast for the next few hours
Implementation
// V.D - Forecast Methods
///////////////
/// Retrieves weather forecast for the next few hours
Future<List<WeatherData>> getHourlyForecast({
required double latitude,
required double longitude
}) async {
try {
final url = Uri.https(_baseUrl, '/data/2.5/forecast', {
'lat': latitude.toString(),
'lon': longitude.toString(),
'appid': apiKey,
'units': 'metric',
'cnt': '5', // Limit to next 5 3-hour intervals
});
final response = await http.get(url);
if (response.statusCode == 200) {
final jsonData = json.decode(response.body);
final list = jsonData['list'] as List;
return list.map((item) {
try {
return WeatherData.fromJson(item);
} catch (e) {
print('Error parsing forecast item: $e');
return null;
}
}).whereType<WeatherData>().toList();
} else {
throw HttpException('Failed to fetch forecast: ${response.statusCode}');
}
} catch (e) {
print('Weather Forecast API error: $e');
rethrow;
}
}