73 lines
1.9 KiB
Dart
73 lines
1.9 KiB
Dart
// 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;
|
|
}
|