getCoinMarketChart method

Future<CoinGeckoResult<List<MarketChartData>>> getCoinMarketChart({
  1. required String id,
  2. required String vsCurrency,
  3. int? days,
  4. String? interval,
})

Get historical market data include price, market cap and 24h volume (granularity auto).

id sets coin id.

vsCurrency sets the target currency of market data (usd, eur, jpy, etc.).

days indicates in how many days to include information. If the parameter is not specified, the maximum possible number of days is assumed.

interval sets data interval. Use CoinMarketChartInterval enumeration as values.

Query path: /coins/{id}/market_chart

Implementation

Future<CoinGeckoResult<List<MarketChartData>>> getCoinMarketChart({
  required String id,
  required String vsCurrency,
  int? days,
  String? interval,
}) async {
  final Map<String, dynamic> queryParameters = {
    'vs_currency': vsCurrency,
    'days': days is int ? days : 'max',
  };
  if (interval is String) {
    queryParameters['interval'] = interval;
  }
  final response = await _client.dio.get(
    '/coins/$id/market_chart',
    queryParameters: queryParameters,
  );
  if (response.statusCode == 200) {
    final list = Helpers.parseMarketChartData(response.data);
    return CoinGeckoResult(list);
  } else {
    return CoinGeckoResult(
      [],
      errorCode: response.statusCode ?? null,
      errorMessage: '${response.statusMessage} - ${response.data.toString()}',
      isError: true,
    );
  }
}