fetchWeather method

Future<void> fetchWeather(
  1. String city
)

Implementation

Future<void> fetchWeather(String city) async {
  if (city.isEmpty) return;

  setState(() {
    _isLoading = true;
    _weather = "Fetching weather...";
  });

  final response = await http.get(Uri.parse(
      'https://api.openweathermap.org/data/2.5/weather?q=$city&appid=211922dc4575ca7aa489da81548a4965&units=metric'));

  if (response.statusCode == 200) {
    final data = jsonDecode(response.body);
    setState(() {
      _weather =
          "${data['main']['temp']}°C | ${data['weather'][0]['description'].toUpperCase()}";
    });
  } else {
    setState(() {
      _weather = "City not found. Try again.";
    });
  }

  setState(() => _isLoading = false);
}