为什么我的价值观不更新在主页吗?颤振



所以我正在制作一个天气应用程序,但当我搜索一个城市时,它不会更新自己,它只是第一次工作,之后如果我搜索任何城市名称,值将不会更新。

这是主主页代码

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不工作,因为它应该我将很高兴如果有人修复这个尽快

下面是RemoteService类的代码,它从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);
}
}
}

加载数据后,posts不再为null,不满足get data方法中提到的条件。请删除getdata中的条件if posts!= null。另外,如果您错误地添加了getdata,请删除上面提到的覆盖。

尝试像更新压力一样更新setstate()中的所有参数。

最新更新