mj1261829
mj1261829

Reputation: 1319

Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<Model>'

I am trying to parse Json data that I get from an online API but get the error mentioned in the title. I looked at similar questions but could not find a solution.

Here is the relevant code:

  class Model {
      final double a;
      final double b;
      final String s;
    
      Model._({this.a, this.b, this.s});
    
      factory Model.fromJson(Map<String, dynamic> json) {
        return new Model._(
          a: json['a'],
          b: json['b'],
          s: json['s'],
        );
      }
    }
    
    void fetchPairValue() async {
    
        final response = await http.get('https://api.1forge.com/quotes?pairs=USD/TRY,EUR/USD,EUR/TRY,CAD/TRY,GBP/TRY,AUD/TRY,JPY/TRY,CHF/TRY,AED/TRY,USD/QAR,USD/BGN,DKK/TRY,USD/SAR,USD/CNY,USD/RUB,NOK/TRY,SEK/TRY'
            '&api_key=KatWbQa9sDFmYQ25LmtAMlGau5xKSWIe');
    
        if (response.statusCode == 200) {
          // If the call to the server was successful, parse the JSON
    
          List<Model> list  = json.decode(response.body).map((data) => Model.fromJson(data))
              .toList();
    
          setState(() {
    
            currencyPair[0][1] = round_to_4(list[0].b).toString();
            currencyPair[0][2] = round_to_4(list[0].a).toString();
    
            for (int i = 1; i < currencyPair.length; i++) {
    
              if(list[i].s.startsWith('USD'))
              {
                currencyPair[i][1] = round_to_4(list[i].b/list[1].b).toString();
                currencyPair[i][2] = round_to_4(list[i].a/list[1].a).toString();
              }
              else {
                currencyPair[i][1] = round_to_4(list[i].b).toString();
                currencyPair[i][2] = round_to_4(list[i].a).toString();
              }
            }
          });
        } else {
          // If that call was not successful, throw an error.
          throw Exception('Failed to load post');
        }
      }

Sample Json Data:

[{"p":1.21856,"a":1.22201,"b":1.2151,"s":"EUR/USD","t":1608934265255},{"p":7.5575,"a":7.5625,"b":7.5525,"s":"USD/TRY","t":1608908143931},{"p":9.26299,"a":9.27256,"b":9.25342,"s":"EUR/TRY","t":1608879625018},{"p":6.037513,"a":6.039437,"b":6.035589,"s":"CAD/TRY","t":1608933871214},{"p":10.297348,"a":10.316695,"b":10.278,"s":"GBP/TRY","t":1608879629130},{"p":5.7738,"a":5.7885,"b":5.7591,"s":"AUD/TRY","t":1608879564069},{"p":0.07303697,"a":0.07308529,"b":0.07298864,"s":"JPY/TRY","t":1608908143937},{"p":8.529457,"a":8.538269,"b":8.520645,"s":"CHF/TRY","t":1608879624835},{"p":2.057672,"a":2.059033,"b":2.056311,"s":"AED/TRY","t":1608908143934},{"p":3.6413,"a":3.642,"b":3.6405,"s":"USD/QAR","t":1608847204796},{"p":1.6069188,"a":1.61497,"b":1.5988675,"s":"USD/BGN","t":1608861813327},{"p":1.2452666,"a":1.2465531,"b":1.24398,"s":"DKK/TRY","t":1608879625024},{"p":3.752353,"a":3.755106,"b":3.7496,"s":"USD/SAR","t":1608879629251},{"p":6.5418,"a":6.5428,"b":6.5408,"s":"USD/CNY","t":1608909993197},{"p":74.06,"a":74.095,"b":74.025,"s":"USD/RUB","t":1608930021562},{"p":0.87736,"a":0.878167,"b":0.876553,"s":"NOK/TRY","t":1608847205092},{"p":0.917155,"a":0.918032,"b":0.916278,"s":"SEK/TRY","t":1608847203927}]

How to fix? Thanks.

Upvotes: 0

Views: 54

Answers (1)

fvillalba
fvillalba

Reputation: 1058

You need to add the generic type to the map function:

    List<Model> list = json
    .decode(response)
    .map<Model>((data) => Model.fromJson(data))
    .toList();

Upvotes: 1

Related Questions