繁体   English   中英

Flutter Api 调用未处理异常

[英]Flutter Api call Unhandled Exception

我对 api 调用有疑问,在项目中我有另一个调用并且它工作,这个调用无论如何都不起作用

// file meteo.dart
class Meteo {
  int id;
  double temperatura;
  String name;
 
 
  Meteo(this.id, this.temperatura, this.name);
 
  Meteo.fromMap(Map<String, dynamic> mappa){
    this.id = mappa['id'];
    this.temperatura = mappa['main']['temp'];
    this.name = mappa['name'].toString();
  }
}

这是响应的 model

  Future ferentinoGet() async {
    var dio = Dio();
    Response response = await
    dio.get('https://api.openweathermap.org/data/2.5/weather?q=ferentino&appid=****************');
    final resJson = response.data;
    datiFerentino = resJson
        .map<Meteo>((mappa) => Meteo.fromMap(mappa))
        .toList();
    print(response.data);
    }

这是 api 电话

[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: Class '_InternalLinkedHashMap<String, dynamic>' has no instance method 'map' with matching arguments.
Receiver: _LinkedHashMap len:13
Tried calling: map<Meteo>(Closure: (dynamic) => Meteo)
Found: map<K2, V2>((K, V) => MapEntry<K2, V2>) => Map<K2, V2>
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      _HomeScreenState.ferentinoGet (package:ferentino/main.dart:223:10)
<asynchronous suspension>
#2      _HomeScreenState.initState (package:ferentino/main.dart:43:5)
#3      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4765:58)
#4      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5)
#5      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
#6      Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
#7      SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6118:14)
#8      Element.<…>

这是错误。

你能帮助我吗?

谢谢

我认为您将 JSON 响应作为List处理,但看起来resJsonMap 但是mapMap方法有不同的原型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM