Home > Software engineering >  Parsing error while trying to insert data in flutter chart
Parsing error while trying to insert data in flutter chart

Time:09-16

Good morning, i tried to parse a json response in flutter, to insert into a chart, but i get this error, List' is not a subtype of type 'List<List>, the chart api is https://api.coingecko.com/api/v3/coins/ethereum/market_chart?vs_currency=usd&days=1, i have tried converting the chartdata to List<List>

what i have tried

 final request = await http
                              .get(Uri.parse(getMarketData(1, 'bitcoin')));

                          if (request.statusCode ~/ 100 == 4 ||
                              request.statusCode ~/ 100 == 5) {
                            throw Exception('Request failed');
                          }

                          List<List<dynamic>> chartData =
                              jsonDecode(request.body)['prices'] as List<List<dynamic>>;

                          print(chartData);

                          series = [
                            charts.Series(
                              id: "crypto price",
                              data: chartData,
                              labelAccessorFn: (List series, _) =>
                                  '${series[0]}',
                              domainFn: (List series, _) => series[0],
                              measureFn: (List series, _) => series[1],
                              colorFn: (List series, _) =>
                                  charts.ColorUtil.fromDartColor(Colors.blue),
                            )
                          ];

CodePudding user response:

First use model to parse the coming data from the api

import 'dart:convert';

CurrencyModel currencyModelFromJson(String str) => 
CurrencyModel.fromJson(json.decode(str));

String currencyModelToJson(CurrencyModel data) => json.encode(data.toJson());

class CurrencyModel {
CurrencyModel({
    this.prices,
    this.marketCaps,
    this.totalVolumes,
});

List<List<double>> prices;
List<List<double>> marketCaps;
List<List<double>> totalVolumes;

factory CurrencyModel.fromJson(Map<String, dynamic> json) => CurrencyModel(
    prices: List<List<double>>.from(json["prices"].map((x) => List<double>.from(x.map((x) => x.toDouble())))),
    marketCaps: List<List<double>>.from(json["market_caps"].map((x) => List<double>.from(x.map((x) => x.toDouble())))),
    totalVolumes: List<List<double>>.from(json["total_volumes"].map((x) => List<double>.from(x.map((x) => x.toDouble())))),
);

Map<String, dynamic> toJson() => {
    "prices": List<dynamic>.from(prices.map((x) => List<dynamic>.from(x.map((x) => x)))),
    "market_caps": List<dynamic>.from(marketCaps.map((x) => List<dynamic>.from(x.map((x) => x)))),
    "total_volumes": List<dynamic>.from(totalVolumes.map((x) => List<dynamic>.from(x.map((x) => x)))),
};

}

then in other class

 final request = await http
                          .get(Uri.parse(getMarketData(1, 'bitcoin')));

                      if (request.statusCode ~/ 100 == 4 ||
                          request.statusCode ~/ 100 == 5) {
                        throw Exception('Request failed');
                      }

                       CurrencyModel chartData = currencyModelFromJson(request.body);


                      // List<List<dynamic>> chartData =
                      //     jsonDecode(request.body)['prices'] as List<List<dynamic>>;

                      print(chartData);

                      series = [
                        charts.Series(
                          id: "crypto price",
                          data: chartData.prices,
                          labelAccessorFn: (List series, _) =>
                              '${series[0]}',
                          domainFn: (List series, _) => series[0],
                          measureFn: (List series, _) => series[1],
                          colorFn: (List series, _) =>
                              charts.ColorUtil.fromDartColor(Colors.blue),
                        )
                      ];
  • Related