簡體   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