Skip to content

Instantly share code, notes, and snippets.

@timbergus
Last active June 7, 2019 11:07
Show Gist options
  • Save timbergus/9a5d7a2d710a5a566152bc53fe31f21a to your computer and use it in GitHub Desktop.
Save timbergus/9a5d7a2d710a5a566152bc53fe31f21a to your computer and use it in GitHub Desktop.
Future<Map> getData(double latitude, double longitude) async {
String api = 'http://api.openweathermap.org/data/2.5/forecast';
String appId = '<YOUR_OWN_API_KEY>';
String url = '$api?lat=$latitude&lon=$longitude&APPID=$appId';
http.Response response = await http.get(url);
Map parsed = json.decode(response.body);
return {
'temperature': toCelsius(parsed['list'][0]['main']['temp']),
'description': parsed['list'][0]['weather'][0]['description'],
};
}
double toCelsius(temp) {
return temp - 273.15;
}
@override
void initState() {
super.initState();
getPosition().then((position) {
getPlacemark(position.latitude, position.longitude).then((data) {
getData(position.latitude, position.longitude).then((weather) {
setState(() {
_locality = data.locality;
_temperature = weather['temperature'];
_description = weather['description'];
});
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment