简体   繁体   中英

Flutter Api call Unhandled Exception

i have a problem with api call, in the project i have another call and it work, this call doesn't work in any way

// 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();
  }
}

This is the model of the response

  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);
    }

This is the api call

[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.<…>

this is the error.

Can you help me?

Thank you

I think you process your JSON response as a List but it looks resJson is a Map . But map method for Map has different prototype.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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