I am trying to display an image in my weather app based on the current weather condition.
I have a jsonConditionsDay variable which stores JSON data fetched from https://www.weatherapi.com/docs/conditions.json.
I have a List<String> conditionsDayImgUrls = []; //network image urls contained in this which stores image urls.
Current weather data is fetched using dio from https://www.weatherapi.com/api-explorer.aspx#forecast and stored in a variable jsonData.
Now when I'm calling the following function String getCurrentConditionsBgImgForDay(String str) I am not getting the desired image from the conditionsDayImgUrls list, instead I'm only getting the first image of the list.
Code for getCurrentConditionsBgImgForDay in global_variables.dart:
String getCurrentConditionsBgImgForDay(String str) {
int i;
for (i = 0; i < jsonConditions.length; i++) {
if (str == jsonConditions[i]["day"]) {
return conditionsDayImgUrls[i];
}
}
//print(i);
return conditionsDayImgUrls[0];
}
The following code in current_weather_screen.dart tries to display the desired weather image based on current weather condition:
Container(
width: screenWidth,
height: screenHeight,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
getCurrentConditionsBgImgForDay(
jsonData["current"]
["condition"]["text"])),
fit: BoxFit.fill,
),
),
),
What is wrong in my code that is preventing it from displaying the correct weather image and how to solve it?