So i was making a weather app , but it does not update itself when i search for a city it just works for the first time and after that if I search any city name the values won't update at all.
Here is the main Homepage code
import 'package:flutter/material.dart';
import 'package:glass_kit/glass_kit.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:percent_indicator/percent_indicator.dart';
import '../model/model.dart';
import '../services/remote_services.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Post? posts;
var isLoaded=true;
var pressure ;
var city = new TextEditingController();
@override
getData(String city) async{
posts=await RemoteService().getPosts(city);
if(posts!=null){
setState((){
isLoaded= true;
print("Weather");
print(posts?.wind.speed);
print(MediaQuery.of(context).size.height);
print(MediaQuery.of(context).size.width);
pressure= posts?.main.pressure;
});
}
}
@override
Widget build(BuildContext context) {
var timezone;
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/Clouds.jpg"),
fit: BoxFit.cover,
),
),
child: SafeArea(
child: SingleChildScrollView(
child: Visibility(
visible: isLoaded,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Row(
children: [
GlassContainer.clearGlass(
borderWidth: 0,
height: 50,
width: MediaQuery.of(context).size.width-70,
borderRadius: BorderRadius.circular(10),
child: TextField(
controller: city,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white
),
decoration: InputDecoration(
hintText: "Enter City",
),
),
),
Expanded(child: SizedBox()),
GlassContainer.clearGlass(
height: 50,
width: 50,
borderWidth: 0,
borderRadius: BorderRadius.circular(10),
child: Center(
child: IconButton(
onPressed: (){
getData(city.text);
},
icon: Icon(Icons.search,color: Colors.white,),
),
),
)
],
),
SizedBox(height: 5,),
Row(
children: [
Column(
children: [
GlassContainer.clearGlass(
height: MediaQuery.of(context).size.height/8.099,
width: MediaQuery.of(context).size.width/3.990,
borderWidth: 0,
borderRadius: BorderRadius.circular(10),
blur: 10,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FaIcon(FontAwesomeIcons.water, color: Colors.white,),
Text(
posts==null?" ":"${posts?.main.humidity}%",
style: TextStyle(
color: Colors.white,
fontSize: 24
),
)
],
),
),
SizedBox(height: 5,),
GlassContainer.clearGlass(
height: MediaQuery.of(context).size.height/8.099,
width: MediaQuery.of(context).size.width/3.990,
borderWidth: 0,
borderRadius: BorderRadius.circular(10),
blur: 10,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FaIcon(FontAwesomeIcons.wind, color: Colors.white,),
Text(
posts==null?" ":"${posts?.wind.speed}",
style: TextStyle(
color: Colors.white,
fontSize: 24
),
),
Text(
"Km/h",
style: TextStyle(
fontSize: 12,
color: Colors.white
),
)
],
),
)
],
),
Expanded(child: SizedBox()),
GlassContainer.clearGlass(
width: MediaQuery.of(context).size.width/1.51102941,
height: MediaQuery.of(context).size.height/3.77731092-10,
borderRadius: BorderRadius.circular(10),
borderWidth: 0,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
posts==null?" ":"${posts?.main.temp} C",
style: TextStyle(
fontSize: 75,
color: Colors.white,
),
),
SizedBox(height: 10,),
Text(
posts==null?"":"Feels like ${posts?.main.feelsLike} C",
style: TextStyle(
fontSize: 18,
color: Colors.white
),
)
],
),
),
),
Expanded(child: SizedBox()),
],
),
SizedBox(height: 7,),
Row(
children: [
GlassContainer.clearGlass(
width: MediaQuery.of(context).size.width/1.51102941,
height: MediaQuery.of(context).size.height/3.77731092-10 12,
borderRadius: BorderRadius.circular(10),
borderWidth: 0,
child: new CircularPercentIndicator(
radius: 90.0,
lineWidth: 13.0,
animation: true,
percent: pressure==null?0:pressure/1084,
center: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FaIcon((pressure!=null)?(pressure>1013)?FontAwesomeIcons.arrowUp:FontAwesomeIcons.arrowDown:FontAwesomeIcons.arrowDown, color: Colors.white,),
Text(
"$pressure mB",
style:
new TextStyle(fontWeight: FontWeight.bold, fontSize: 30, color: Colors.white),
),
],
),
circularStrokeCap: CircularStrokeCap.round,
progressColor: Colors.white,
),
),
Expanded(child: SizedBox()),
Column(
children: [
GlassContainer.clearGlass(
height: MediaQuery.of(context).size.height/8.099,
width: MediaQuery.of(context).size.width/3.990,
borderWidth: 0,
borderRadius: BorderRadius.circular(10),
blur: 10,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FaIcon(FontAwesomeIcons.temperatureArrowUp, color: Colors.white,),
Text(
"${posts?.main.tempMax} C",
style: TextStyle(
color: Colors.white,
fontSize: 24
),
)
],
),
),
SizedBox(height: 18,),
GlassContainer.clearGlass(
height: MediaQuery.of(context).size.height/8.099,
width: MediaQuery.of(context).size.width/3.990,
borderWidth: 0,
borderRadius: BorderRadius.circular(10),
blur: 10,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FaIcon(FontAwesomeIcons.temperatureArrowDown, color: Colors.white,),
Text(
"${posts?.main.tempMin} C",
style: TextStyle(
color: Colors.white,
fontSize: 24
),
)
],
),
),
],
),
Expanded(child: SizedBox())
],
),
// GlassContainer.clearGlass(height: height, width: width)
],
),
),
),
),
),
),
);
}
}
setState is not working as it should i would be glad if some fixed this as soon as possible
Here is the code for the RemoteService class, it requests from the Uri.
import 'package:http/http.dart' as http;
import '../model/model.dart';
class RemoteService{
Future <Post?> getPosts(String city) async{
var client = http.Client();
var uri = Uri.parse("https://api.openweathermap.org/data/2.5/weather?q=$city&appid=5e749d88f2df02cacc9be6abf8088531&units=metric");
var response = await client.get(uri);
if (response.statusCode==200){
var json=response.body;
return postFromJson(json);
}
}
}
CodePudding user response:
Once the data is loaded posts
is no more null which doesnt satisfy the condition mentioned in the get data method. Please remove the condition if posts!= null
in getdata. Also please remove override mentioned above getdata if you have added it by mistake.
CodePudding user response:
Try updating all parameters in setstate() just like pressure. This will work.