json - 为什么我会收到此错误?在 null 上调用了方法“[]”
问题描述
我在从 openWeatherMap 访问一些天气数据时遇到问题。我收到此错误。
在 null 上调用了方法“[]”。接收者:null 尝试调用:
这是我的代码
class LocationScreen extends StatefulWidget {
final locationWeather;
LocationScreen({this.locationWeather});
@override
_LocationScreenState createState() => _LocationScreenState();
}
class _LocationScreenState extends State<LocationScreen> {
dynamic weatherDescription;
double? temperature;
double? minTemperature;
double? maxTemperature;
double? humidity;
String? cityName;
@override
void initState() {
super.initState();
updateUI(widget.locationWeather);
}
void updateUI(dynamic weatherData) {
weatherDescription = weatherData['weather'][0]['description'];
temperature = weatherData['main']['temp'];
minTemperature = weatherData['main']['temp_min'];
maxTemperature = weatherData['main']['temp_max'];
humidity = weatherData['main']['humidity'];
cityName = weatherData['name'];
}
@override
Widget build(BuildContext context) {
return Scaffold()
我想我在尝试访问它时有正确的 ['weather'][0]['description'] 语法?我正在努力提高我的编码技能和自学能力。我尝试在这里搜索并查看文档,但似乎并没有完全理解。
解决方案
推荐阅读
- wordpress - 在 WP 中打开带有密码保护的 Pdf
- c - 我应该为哈希表中的存储桶使用什么数据结构?
- c# - 如何删除或隐藏枢轴的页面选择
- python - 来自 scikit-learn 随机森林的令人困惑的概率
- awk - 在 awk 中使用标准输出作为输入
- asp.net - 使用 IAuthenticationFilter 时 HttpContext.Current 为空
- javascript - 渲染器的回调没有被调用,这是怎么回事?
- c - 嵌套在openmp中的lops
- jquery - 如何使用 ajax jquery 获取最终选择值?
- c - 在 C 中使用带有命令行参数的 system()