MIF_E31231027/smart_cycling/lib/services/weather_service.dart

41 lines
1.1 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
import '../utils/api_keys.dart';
class WeatherService {
static const String _baseUrl = "https://api.openweathermap.org/data/2.5/weather";
Future<Map<String, dynamic>?> fetchWeather(double lat, double lon) async {
try {
final response = await http.get(
Uri.parse("$_baseUrl?lat=$lat&lon=$lon&appid=${ApiKeys.openWeatherApiKey}&units=metric&lang=id"),
);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else {
return null;
}
} catch (e) {
return null;
}
}
Future<Map<String, dynamic>?> fetchForecast(double lat, double lon) async {
try {
final String forecastUrl = "https://api.openweathermap.org/data/2.5/forecast";
final response = await http.get(
Uri.parse("$forecastUrl?lat=$lat&lon=$lon&appid=${ApiKeys.openWeatherApiKey}&units=metric&lang=id"),
);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else {
return null;
}
} catch (e) {
return null;
}
}
}