【发布时间】:2020-05-15 03:03:52
【问题描述】:
class _LocationScreenState extends State<LocationScreen> {
WeatherModel weather = WeatherModel();
String weatherMessage;
String weatherIcon;
String cityName;
int temperature;
@override
void initState() {
super.initState();
updateUI(widget.locationWeather);
}
void updateUI(dynamic weatherData) {
setState(() {
temperature=weatherData['main']['temp'].toInt();
var condition = weatherData['weather'][0]['id'];
cityName=weatherData['name'];
weatherIcon=weather.getWeatherIcon(condition);
weatherMessage=weather.getMessage(temperature);
});
}
嗨,我对 setState() 的作用感到困惑。如果温度或条件的值或 setState 内部的任何变化,setState() 会触发自身更新 UI 并使用更新的温度或条件构建 UI,还是我必须自己调用 updateUI 函数来调用 setState 并更新 UI?
【问题讨论】: