fetchChartData static method

Future<List<Series<CryptoPrice, DateTime>>> fetchChartData(
  1. List<Cryptocurrency> cryptocurrencies,
  2. String daysToFetch
)

Implementation

static Future<List<charts.Series<CryptoPrice, DateTime>>> fetchChartData(
    List<Cryptocurrency> cryptocurrencies, String daysToFetch) async {
  //TODO change currency too - vs_currency=usd
  final List<charts.Series<CryptoPrice, DateTime>> allChartData = [];

  int id = 0;

  for (var cryptocurrency in cryptocurrencies) {
    var rawData = await DataFetcher.fetchUrlData(
        "https://api.coingecko.com/api/v3/coins/${cryptocurrency.id}/market_chart?vs_currency=usd&days=$daysToFetch&interval=daily");

    var data = jsonDecode(rawData);

    List<CryptoPrice> singleChartData = [];

    for (var element in data["prices"]) {
      var rawDateTime = DateTime.fromMillisecondsSinceEpoch(element[0]);
      var cryptoDate =
          DateTime(rawDateTime.year, rawDateTime.month, rawDateTime.day);
      var cryptoPrice = element[1];

      singleChartData.add(CryptoPrice(cryptoDate, cryptoPrice));
    }

    charts.Color color = chartColorFromId(id++);

    allChartData.add(charts.Series<CryptoPrice, DateTime>(
      id: cryptocurrency.id,
      colorFn: (_, __) => color,
      domainFn: (CryptoPrice priceOverTime, _) => priceOverTime.time,
      measureFn: (CryptoPrice priceOverTime, _) => priceOverTime.price,
      data: singleChartData,
    ));
  }

  return allChartData;
}