Untitled

mail@pastecode.io avatar
unknown
dart
7 months ago
1.5 kB
1
Indexable
Never
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:http/http.dart';


class worker {
  String? location;
  worker({this.location}){
    location=this.location;

  }

  String? temp;
  String? humidity;
  String? air_speed;
  String? describtion;

  Future <void> getData() async{

    try{
      Response response = await http
          .get(Uri.parse('https://api.openweathermap.org/data/2.5/weather?q=$location&appid=985298f7178fa68eff25a080601ba924'));
      Map data = jsonDecode(response.body);

      //Temp ,Humidity,
      Map temp_data = data['main'];
      double get_temp = temp_data['temp'];
      String get_humidity=temp_data['humidity'].toString();


      //wind speed
      Map wind = data['wind'];
      double get_wind_speed = wind['speed'];

      //getting describtion
      List weather_list_data = data['weather'];
      Map weather_map_data = weather_list_data[0];
      String get_describtion = weather_map_data['description'];

      //assingning values
      temp=get_temp.toString();
      humidity=get_humidity;
      air_speed=get_wind_speed.toString();
      describtion=get_describtion;

    }catch(e)
    {
      temp="Wrong location or Location doesn't match";
      humidity="Wrong location or Location doesn't match";
      air_speed="Wrong location or Location doesn't match";
      describtion="Wrong location or Location doesn't match";
    }




  }

}