asynchronous - 未处理的异常:类型 '_InternalLinkedHashMap' 不是类型 'Future 的子类型'
问题描述
我收到错误消息:
Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Future <dynamic>`
_LocationScreenState.build.<anonymous closure>
(package:clima/screens/location_screen.dart:71:32)
<asynchronous suspension>
被精确定位的线在这个里面:
FlatButton(
onPressed: () async {
var weatherData = await weather.getLocationWeather();
updateUI(weatherData); //<==it's this line
},
weather.getLocationWeather()
返回类型为 JSON 的变量,Future<dynamic>.
并且在调试器中对其进行评估,它是预期的 JSON。方法upDateUI()
开始
void updateUI(Future<dynamic> weatherDataFuture) async {
final weatherData = await weatherDataFuture;
然后继续解析生成的weatherData。它在程序的早期被调用并按预期工作。我确信这个问题与我永无止境但仍在继续为理解异步编程而奋斗:-)
解决方案
获取由返回的await weather.getLocationWeather()
未来getLocationWeather
并等待它完成一个值。该值是一张地图。
然后,您使用该地图值进行调用updateUI
,即使它期待未来。该变量的类型为dynamic
,因此您不会收到静态警告,只会收到运行时错误。
onPressed: () async {
var weatherData = await weather.getLocationWeather();
updateUI(weatherData); //<==it's this line
尝试删除await
. 然后weatherData
将是 a Future<dynamic>
,这正是updateUI
. (然后您也可以async
从函数中删除 )。