【发布时间】:2021-02-25 02:10:06
【问题描述】:
这段代码我有这个问题,我尝试将变量转换为动态但没有成功,希望有人知道解决方案,谢谢请帮助。
这段代码我有这个问题,我尝试将变量转换为动态但没有成功,希望有人知道解决方案,谢谢请帮助。
import 'package:flutter/material.dart';
import 'package:clima/utilities/constants.dart';
import 'package:clima/services/weather.dart';
class LocationScreen extends StatefulWidget {
LocationScreen({this.locationWeather});
final locationWeather;
@override
_LocationScreenState createState() => _LocationScreenState();
}
class _LocationScreenState extends State<LocationScreen> {
WeatherModel weather = WeatherModel();
int temperature;
String weatherIcon;
String cityName;
String weatherMessage;
@override
void initState() {
// ignore: todo
// TODO: implement initState
super.initState();
updateUI(widget.locationWeather);
}
// ignore: non_constant_identifier_names
void updateUI(dynamic WeatherData) {
setState(() {
temperature = WeatherData['main']['temp'];
var condition = WeatherData['weather'][0]['description'];
weatherIcon = weather.getWeatherIcon(condition);
weatherMessage = weather.getMessage(temperature);
cityName = WeatherData['name'];
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/location_background.jpg'),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(
Colors.white.withOpacity(0.8), BlendMode.dstATop),
),
),
constraints: BoxConstraints.expand(),
child: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
FlatButton(
onPressed: () {},
child: Icon(
Icons.near_me,
size: 50.0,
),
),
FlatButton(
onPressed: () {},
child: Icon(
Icons.location_city,
size: 50.0,
),
),
],
),
Padding(
padding: EdgeInsets.only(left: 15.0),
child: Row(
children: <Widget>[
Text(
'$temperature°',
style: kTempTextStyle,
),
Text(
weatherIcon,
style: kConditionTextStyle,
),
],
),
),
Padding(
padding: EdgeInsets.only(right: 15.0),
child: Text(
'$weatherMessage as in $cityName',
textAlign: TextAlign.right,
style: kMessageTextStyle,
),
),
],
),
),
),
);
}
}
【问题讨论】:
-
这是 Java 还是 Javascript?一个本质上是一个玩具,专为编写小段代码而设计,传统上被缺乏经验的程序员使用和滥用。另一种是用于网络浏览器的脚本语言。
-
嘿,主要是你的模特。您可能会为 WeatherData 中的“临时”键返回整数。但在您也发布您的模型之前,无法确认。
-
@Pushpendra 检查这个codeshare.io/24r69L
-
您的 WeatherData (我假设为 json)似乎提供了一个字符串而不是一个 int 来表示温度和/或条件。尝试将 int.parse(condition) 和 int.parse(temperature) 传递给 WeatherModel。
标签: javascript java flutter dart