AtmoSphere/lib/repositories/weather_repository.dart
2026-01-16 18:22:32 +08:00

28 lines
816 B
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
import '../constants.dart';
import '../models/weather_models.dart';
class WeatherRepository {
final http.Client client;
WeatherRepository({required this.client});
Future<WeatherModel> fetchWeather(String city) async {
final url =
'$weatherApiBaseUrl/forecast.json?key=$weatherApiKey&q=$city&days=7&aqi=no&alerts=no';
try {
final response = await client.get(Uri.parse(url));
if (response.statusCode == 200) {
final data = json.decode(response.body);
return WeatherModel.fromJson(data);
} else {
throw Exception(
'Failed to load weather data (Code: ${response.statusCode})',
);
}
} catch (e) {
throw Exception('Failed to fetch weather: $e');
}
}
}