【发布时间】:2019-12-29 00:56:48
【问题描述】:
我得到错误:
I/flutter ( 4091): The following NoSuchMethodError was thrown building Builder: I/flutter ( 4091): Class 'Future' has no instance method '[]'. I/flutter ( 4091): Receiver: Instance of 'Future' I/flutter ( 4091): Tried calling: []("main")
并追溯到:
void updateUI(dynamic weatherData) {
setState(() {
if (weather Data==null) {
//error checking code that's not called; weatherData object isn't null
}
temperature = weatherData['main']['temp'];
weatherMessage = weather.getMessage(temperature);
var condition = weatherData['weather'][0]['id'];
weatherIcon = weather.getWeatherIcon(condition);
cityName = weatherData['name'];
});
}
变量在别处初始化。这个问题与我显然不了解 Flutter Futures 和/或异步编程有关。 weatherData 对象是从 openWeatherMaps API 中提取的 JSON 字符串。如果我在创建它的地方调用print(weatherData),我会得到预期的 JSON 字符串。如果我在上面的方法中调用print(weatherData),我会得到`Instance of Future'。
【问题讨论】:
标签: asynchronous flutter dart