首页 > 解决方案 > 为什么我会收到此错误?在 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'] 语法?我正在努力提高我的编码技能和自学能力。我尝试在这里搜索并查看文档,但似乎并没有完全理解。

标签: jsonflutteropenweathermap

解决方案


推荐阅读