Successfully pulling a good weather report. Now to add the heat index and wind chill calculations.
This commit is contained in:
@ -8,7 +8,7 @@ import 'dart:convert';
|
||||
|
||||
// Generic method to hit a GET request and return the response.
|
||||
Future<String> hitAPI(String url) async {
|
||||
if (debug) debugPrint(url);
|
||||
if (debug) debugPrint("DEBUG: URL is '$url'.");
|
||||
|
||||
http.Response response = await http.get(Uri.parse(url));
|
||||
|
||||
@ -16,10 +16,17 @@ Future<String> hitAPI(String url) async {
|
||||
if (response.statusCode == 200) {
|
||||
data = response.body;
|
||||
var decodedData = jsonDecode(data);
|
||||
if (debugVerbose) debugPrint(decodedData.toString());
|
||||
data = decodedData.toString();
|
||||
//data = decodedData.toString();
|
||||
//data = jsonEncode(decodedData);
|
||||
if (debugVerbose) {
|
||||
debugPrint("DEBUG-VERBOSE: Response data: \n\n$data\n");
|
||||
}
|
||||
} else {
|
||||
if (debug) debugPrint(response.statusCode.toString());
|
||||
if (debug) {
|
||||
debugPrint(
|
||||
"DEBUG: Response failed with code '${response.statusCode.toString()}'",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
@ -38,8 +45,3 @@ Future<String> hitOpenWeatherZip(String zipCode, String? countryCode) async {
|
||||
String data = await hitAPI(url);
|
||||
return data;
|
||||
}
|
||||
|
||||
// Convert the Future value into a usable value.
|
||||
loadWeather() async {
|
||||
return await hitOpenWeatherZip("47630", "US");
|
||||
}
|
||||
|
@ -1 +1,72 @@
|
||||
// Functions related to parsing JSON strings and objects.
|
||||
|
||||
import '/var/config.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
String formatOpenWeatherData(var data) {
|
||||
String location = pullOpenWeatherCity(data),
|
||||
temp = pullOpenWeatherTemp(data),
|
||||
conditions = pullOpenWeatherConditions(data),
|
||||
windSpeed = pullOpenWeatherWind(data),
|
||||
humidity = pullOpenWeatherHumidity(data),
|
||||
windChill = "",
|
||||
heatIndex = "",
|
||||
comfort = "";
|
||||
|
||||
// Always ask for Imperial units in API request
|
||||
final String tempUnits = 'F', windUnits = 'mph', humidityUnits = '%';
|
||||
|
||||
String text =
|
||||
"$location is $temp$tempUnits and $conditions"
|
||||
" with a wind speed of $windSpeed$windUnits"
|
||||
" and humidity of $humidity$humidityUnits."
|
||||
" $windChill$heatIndex"
|
||||
" $comfort";
|
||||
|
||||
final String doubleSpace = " ", singleSpace = " ";
|
||||
while (text.contains(doubleSpace)) {
|
||||
text = text.replaceAll(doubleSpace, singleSpace);
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
String pullOpenWeatherCity(var data) {
|
||||
String location = "${data['city']['name']} (${data['city']['country']})";
|
||||
if (debug) {
|
||||
debugPrint("DEBUG: location = '$location'");
|
||||
}
|
||||
return location;
|
||||
}
|
||||
|
||||
String pullOpenWeatherTemp(var data) {
|
||||
String temp = data['list'][0]['main']['temp'].toString();
|
||||
if (debug) {
|
||||
debugPrint("DEBUG: temp = '$temp'");
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
String pullOpenWeatherConditions(var data) {
|
||||
String conditions = data['list'][0]['weather'][0]['description'].toString();
|
||||
if (debug) {
|
||||
debugPrint("DEBUG: conditions = '$conditions'");
|
||||
}
|
||||
return conditions;
|
||||
}
|
||||
|
||||
String pullOpenWeatherWind(var data) {
|
||||
String wind = data['list'][0]['wind']['speed'].toString();
|
||||
if (debug) {
|
||||
debugPrint("DEBUG: wind = '$wind'");
|
||||
}
|
||||
return wind;
|
||||
}
|
||||
|
||||
String pullOpenWeatherHumidity(var data) {
|
||||
String humidity = data['list'][0]['main']['humidity'].toString();
|
||||
if (debug) {
|
||||
debugPrint("DEBUG: humidity = '$humidity'");
|
||||
}
|
||||
return humidity;
|
||||
}
|
||||
|
Reference in New Issue
Block a user