From d70dfb3843da31697b18dc00b973b1c71822fa27 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 18 Aug 2021 23:35:04 +0100 Subject: [PATCH] Fixed ocassional API error on first refresh, moved networking out of weather class --- lib/constants.dart | 4 ++ lib/models/weather.dart | 54 +++++++++++--------------- lib/screens/main_display.dart | 11 ++---- lib/utils/network.dart | 27 ++++++------- lib/widgets/daily_weather_display.dart | 1 - 5 files changed, 42 insertions(+), 55 deletions(-) diff --git a/lib/constants.dart b/lib/constants.dart index fa40fcf..8a2e0b1 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -5,6 +5,10 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart'; String openWeatherURL = "https://api.openweathermap.org/data/2.5/onecall"; String reverseGeocoding = "http://api.openweathermap.org/geo/1.0/reverse"; +//Future settings switch +String appLang = "en"; +String appUnits = "metric"; + //App Icons Icon kLightningIcon = Icon( FontAwesomeIcons.bolt, diff --git a/lib/models/weather.dart b/lib/models/weather.dart index 04823cf..446963d 100644 --- a/lib/models/weather.dart +++ b/lib/models/weather.dart @@ -4,9 +4,11 @@ import 'package:http/http.dart'; import 'package:weatherapp/constants.dart'; import 'package:weatherapp/credentials.dart'; -import 'package:weatherapp/utils/location.dart'; import 'package:weatherapp/models/dailyweather.dart'; +import 'package:weatherapp/utils/location.dart'; import 'package:weatherapp/utils/daynight.dart'; +import 'package:weatherapp/utils/network.dart'; +import 'package:weatherapp/widgets/daily_weather_display.dart'; //Setting icon and background image class WeatherDisplayData{ @@ -40,18 +42,13 @@ class WeatherData{ //Daily weather values dynamic maxTemp; dynamic minTemp; - - //Future settings switch - String appLang = "en"; - String appUnits = "metric"; + dynamic weatherResponse; Future getCurrentTemperature() async{ - Response response = await get( - //One call API. Returns more data than the standard data - Uri.parse( - '$openWeatherURL?lat=${locationData.latitude}&lon=${locationData.longitude}&exclude=&appid=$apiKey&units=$appUnits&lang=$appLang' - ) - ); + Response response = await + NetworkService().getCurrentTemperature(locationData); + + weatherResponse = response; //Return the weather values if(response.statusCode == 200){ @@ -74,26 +71,21 @@ class WeatherData{ } //Generating a DailyWeather instance for daily weather cards - void getDailyWeather(dynamic weatherData) { - print("weather: getDailyWeather"); - if(weatherData['daily'] != null){ - print("weather:not null"); - List jsonDays = weatherData['daily']; - jsonDays.forEach((day) { - dailyWeatherCards.add( - DailyWeather( - weekday: kWeekdays[ - DateTime.fromMillisecondsSinceEpoch(day['dt'] * 1000).weekday]?? '', - //conditionWeather: day['weather'][0]['id'], - maxTemp: - day['temp']['max'].round(), - minTemp: day['temp']['min'].round(), - ), - ); - }); - }else{ - print("weather: Daily null"); - } + //ToDo: I wonder if it doesn't work since I use weatherData, instead of the response object + void getDailyWeather(weatherResponse) { + List jsonDays = weatherResponse['daily']; + jsonDays.forEach((day) { + dailyWeatherCards.add( + DailyWeather( + weekday: kWeekdays[ + DateTime.fromMillisecondsSinceEpoch(day['dt'] * 1000).weekday]?? '', + //conditionWeather: day['weather'][0]['id'], + maxTemp: + day['temp']['max'].round(), + minTemp: day['temp']['min'].round(), + ), + ); + }); } //Icon changing based on weather diff --git a/lib/screens/main_display.dart b/lib/screens/main_display.dart index c161a86..1b1c615 100644 --- a/lib/screens/main_display.dart +++ b/lib/screens/main_display.dart @@ -1,8 +1,10 @@ import 'package:flutter/material.dart'; +import 'package:http/http.dart'; import 'package:weatherapp/utils/daynight.dart'; import 'package:weatherapp/utils/string_formatter.dart'; import 'package:weatherapp/utils/location.dart'; +import 'package:weatherapp/utils/network.dart'; import 'package:weatherapp/models/geocoding.dart'; import 'package:weatherapp/models/weather.dart'; import 'package:weatherapp/models/dailyweather.dart'; @@ -61,13 +63,10 @@ class _MainDisplayState extends State { weatherData.getWeatherDisplayData(); backgroundImage = weatherDisplayData.weatherImage; weatherDisplayIcon = weatherDisplayData.weatherIcon; - - //Daily forecast - //weatherData.getDailyWeather(weatherData); }); } - void refreshInfo() async{ + Future refreshInfo() async{ //Refresh Location, Update Geodata and Refresh Weather newLocation = LocationHelper(); await newLocation.getCurrentLocation(); @@ -77,8 +76,6 @@ class _MainDisplayState extends State { newWeather = WeatherData(locationData: newLocation); await newWeather.getCurrentTemperature(); - - newWeather.getDailyWeather(newWeather); } @override @@ -157,7 +154,7 @@ class _MainDisplayState extends State { floatingActionButton: FloatingActionButton( onPressed: () async { //Refresh Location, Update Geodata and Refresh Weather - refreshInfo(); + await refreshInfo(); print("Updated UI"); diff --git a/lib/utils/network.dart b/lib/utils/network.dart index 7e3e70f..167bd05 100644 --- a/lib/utils/network.dart +++ b/lib/utils/network.dart @@ -1,22 +1,17 @@ -import 'dart:convert' as convert; - -import 'package:http/http.dart' as http; +import 'package:http/http.dart'; import 'package:weatherapp/credentials.dart'; +import 'package:weatherapp/constants.dart'; -String key = apiKey; - -//ToDo: Currently unused due to it needing required, and the URL in the method that evokes it class NetworkService{ - final String url; - NetworkService(String s, {required this.url}); - - Future fetchAPI(String url) async{ - final response = await http.get(url as Uri); + String key = apiKey; - if(response.statusCode == 200){ - return convert.jsonDecode(response.body); - } else{ - throw Exception('Failed to fetch API'); - } + Future getCurrentTemperature(locationData) async{ + Response response = await get( + //One call API. Returns more data than the standard data + Uri.parse( + '$openWeatherURL?lat=${locationData.latitude}&lon=${locationData.longitude}&exclude=&appid=$apiKey&units=$appUnits&lang=$appLang' + ) + ); + return response; } } \ No newline at end of file diff --git a/lib/widgets/daily_weather_display.dart b/lib/widgets/daily_weather_display.dart index e558742..ad56d03 100644 --- a/lib/widgets/daily_weather_display.dart +++ b/lib/widgets/daily_weather_display.dart @@ -1,5 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:weatherapp/models/weather.dart'; import 'package:weatherapp/utils/daynight.dart'; import 'package:weatherapp/constants.dart';