当前位置: 首页 > 知识库问答 >
问题:

未处理的异常:类型_InternalLinkedHashMap不是类型未来的子类型

慕承允
2023-03-14

我收到了错误消息:

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()返回类型为Future的变量

  void updateUI(Future<dynamic> weatherDataFuture) async {
    final weatherData = await weatherDataFuture;

然后继续解析生成的天气数据。它在程序的前面被调用,并按预期工作。我确信这个问题与我永无止境但仍在继续的理解异步编程的战斗有关:-)


共有3个答案

马阳晖
2023-03-14

获取数据的返回值需要进行转换。我建议在flutter留档中查看JSON教程。

    FlatButton(
       onPressed: () async {
         var weatherData = Map<String, dynamic>.from(await weather.getLocationWeather());
         updateUI(weatherData); //<==it's this line
    },
void updateUI(Map<String, dynamic> weatherData) async {
    ...

=====基于评论的编辑=====

FlatButton(
    onPressed: () async {
      var weatherData = SynchronousFuture(Map<String, dynamic>.from(await weather.getLocationWeather()));
      updateUI(weatherData); //<==it's this line
},
void updateUI(Future<Map<String, dynamic>> weatherData) async {
    ...
鲁靖
2023-03-14

试试这个:

final weatherData;
Future<Map<String, dynamic>> weatherDataFuture async{
  return weather.getLocationWeather();
}

FlatButton(
  onPressed: () {
  weatherDataFuture.then((value){
   weatherData=value.values;
 });
} 
) 
晋弘义
2023-03-14

等待weather.getLocationWeather()获取由getLocationWeather返回的未来,并等待它完成为值。该值是一个映射。

然后使用该映射值调用updateUI,即使它期望未来。变量类型为dynamic,因此不会收到静态警告,只会收到一个运行时错误。

   onPressed: () async {
     var weatherData = await weather.getLocationWeather();
     updateUI(weatherData); //<==it's this line

尝试删除wait。那么weatherData将是的未来

 类似资料: