setState 在颤动的 Android Studio 中无法识别
setState not recognized in flutter Androidstudio
我在互联网上看到了答案,但在所有这些答案中,OP 使用的是 StatelessWidget 而不是 StatefulWidget。我使用了 StatefulWidget,但仍然无法识别 setState()
请参考代码第43行。
嗨!我正在尝试学习 flutter 并正在制作一个天气预报应用程序作为一个项目
为了实现这一点,我正在关注 youtube 上的教程,youtuber 在使用 visual studio 代码时没有出现任何错误,另一方面,我正在使用 Android studio
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() => runApp(
MaterialApp(
title: "Weather App",
home: Home(),
)//MaterialApp
);
class Home extends StatefulWidget {
@override
State<StatefulWidget>createState()
{
return _HomeState();
}
@override
Widget build(BuildContext context) {
// TODO: implement build
throw UnimplementedError();
}
}
class _HomeState extends State<Home>{
var temp;
var description;
var currently;
var humidity;
var windSpeed;
Future getWeather() async {
http.Response response = await http.get(
"https://api.openweathermap.org/data/2.5/weather?q=Delhi&APPID=4570e05ab53aad5b3893831a809ee608");
var results = jsonDecode(response.body);
setState((){ //<- THIS IS NOT RECOGNIZED
this.temp = results['main']['temp'];
this.description = results['weather'][0]['description'];
this.currently = results['weather'][0]['main'];
this.humidity = results['main']['humidity'];
this.windSpeed = results['main']['speed'];
})
}
@override
Widget build (BuildContext context)
{
return Scaffold(
body: Column(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height / 3,
width: MediaQuery.of(context).size.width,
color: Colors.red,
child: Column(
mainAxisAlignment: MainAxisAlignment.center ,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(bottom: 10.0),
child: Text(
"Currently in Boston",
style: TextStyle
(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.w600
),
),
),
Text(
"52\u00B0",
style:TextStyle(
color: Colors.white,
fontSize:40.0,
fontWeight: FontWeight.w600
),
),
Padding(
padding: EdgeInsets.only(bottom: 10.0),
child: Text(
"Rain",
style: TextStyle
(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.w600
),
),
),
],
),
),
Expanded(child: Padding(
padding: EdgeInsets.all(20.0),
child:ListView(
children: <Widget>[
ListTile(
leading: FaIcon(FontAwesomeIcons.temperatureHalf),
title: Text("Temprature"),
trailing: Text("52\u00B0"),
),
ListTile(
leading: FaIcon(FontAwesomeIcons.cloud),
title: Text("Weather"),
trailing: Text("Weather"),
),
ListTile(
leading: FaIcon(FontAwesomeIcons.sun),
title: Text("Humidity"),
trailing: Text("12"),
),
ListTile(
leading: FaIcon(FontAwesomeIcons.wind),
title: Text("Wind Speed"),
trailing: Text("12"),
),
],
)
))
],
),
);
}
}
我认为您需要调用函数 getWeather,例如在初始化状态方法中。
@override
initState() {
getWeather();
}
已修复!少了一个分号。
你的 setState((){...})[ 末尾缺少一个 ;
=11=]
我认为你应该使用 Uri.parse()
:
http.Response response = await http.get(
Uri.parse("https://api.openweathermap.org/data/2.5/weather?q=Delhi&APPID=4570e05ab53aad5b3893831a809ee608"));
我在互联网上看到了答案,但在所有这些答案中,OP 使用的是 StatelessWidget 而不是 StatefulWidget。我使用了 StatefulWidget,但仍然无法识别 setState()
请参考代码第43行。
嗨!我正在尝试学习 flutter 并正在制作一个天气预报应用程序作为一个项目 为了实现这一点,我正在关注 youtube 上的教程,youtuber 在使用 visual studio 代码时没有出现任何错误,另一方面,我正在使用 Android studio
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() => runApp(
MaterialApp(
title: "Weather App",
home: Home(),
)//MaterialApp
);
class Home extends StatefulWidget {
@override
State<StatefulWidget>createState()
{
return _HomeState();
}
@override
Widget build(BuildContext context) {
// TODO: implement build
throw UnimplementedError();
}
}
class _HomeState extends State<Home>{
var temp;
var description;
var currently;
var humidity;
var windSpeed;
Future getWeather() async {
http.Response response = await http.get(
"https://api.openweathermap.org/data/2.5/weather?q=Delhi&APPID=4570e05ab53aad5b3893831a809ee608");
var results = jsonDecode(response.body);
setState((){ //<- THIS IS NOT RECOGNIZED
this.temp = results['main']['temp'];
this.description = results['weather'][0]['description'];
this.currently = results['weather'][0]['main'];
this.humidity = results['main']['humidity'];
this.windSpeed = results['main']['speed'];
})
}
@override
Widget build (BuildContext context)
{
return Scaffold(
body: Column(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height / 3,
width: MediaQuery.of(context).size.width,
color: Colors.red,
child: Column(
mainAxisAlignment: MainAxisAlignment.center ,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(bottom: 10.0),
child: Text(
"Currently in Boston",
style: TextStyle
(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.w600
),
),
),
Text(
"52\u00B0",
style:TextStyle(
color: Colors.white,
fontSize:40.0,
fontWeight: FontWeight.w600
),
),
Padding(
padding: EdgeInsets.only(bottom: 10.0),
child: Text(
"Rain",
style: TextStyle
(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.w600
),
),
),
],
),
),
Expanded(child: Padding(
padding: EdgeInsets.all(20.0),
child:ListView(
children: <Widget>[
ListTile(
leading: FaIcon(FontAwesomeIcons.temperatureHalf),
title: Text("Temprature"),
trailing: Text("52\u00B0"),
),
ListTile(
leading: FaIcon(FontAwesomeIcons.cloud),
title: Text("Weather"),
trailing: Text("Weather"),
),
ListTile(
leading: FaIcon(FontAwesomeIcons.sun),
title: Text("Humidity"),
trailing: Text("12"),
),
ListTile(
leading: FaIcon(FontAwesomeIcons.wind),
title: Text("Wind Speed"),
trailing: Text("12"),
),
],
)
))
],
),
);
}
}
我认为您需要调用函数 getWeather,例如在初始化状态方法中。
@override
initState() {
getWeather();
}
已修复!少了一个分号。
你的 setState((){...})[ 末尾缺少一个 ;
=11=]
我认为你应该使用 Uri.parse()
:
http.Response response = await http.get(
Uri.parse("https://api.openweathermap.org/data/2.5/weather?q=Delhi&APPID=4570e05ab53aad5b3893831a809ee608"));