我在互聯網上看到了答案,但在所有答案中,OP 都使用了 StatelessWidget 而不是 StatefulWidget。我使用了 StatefulWidget 但仍然無法識別 setState()
請參閱代碼的第 43 行。
你好!我正在嘗試學習顫振并且正在將天氣預報應用程式作為一個專案來實作我正在關注 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"),
),
],
)
))
],
),
);
}
}
uj5u.com熱心網友回復:
在您的setState((){...});末尾缺少一個;
我認為你應該使用Uri.parse():
http.Response response = await http.get(
Uri.parse("https://api.openweathermap.org/data/2.5/weather?q=Delhi&APPID=4570e05ab53aad5b3893831a809ee608"));
uj5u.com熱心網友回復:
我認為你需要呼叫你的函式 getWeather,例如在 init state 方法中。
@override
initState() {
getWeather();
}
uj5u.com熱心網友回復:
解決它!少了一個分號。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/485155.html
上一篇:我確實在visualstudio代碼中運行了一個python代碼,但我沒有看到任何結果,但是我只看到一個錯誤,我該如何修復它?
