parseMarketChartData static method
Parses list of MarketChartData from dynamic
Implementation
static List<MarketChartData> parseMarketChartData(dynamic d) {
final map = Convert.toMapN<String, dynamic>(d);
if (map == null) {
return [];
}
// prices
final coinData = SplayTreeMap<DateTime, MarketChartData>();
var workingList = Convert.toListN(map['prices']);
workingList?.forEach((e) {
final item = Convert.toListN(e);
if (item != null) {
final date = Convert.toDateTimeN(item[0], unit: TimeUnit.milliseconds);
if (date != null) {
final price = Convert.toDoubleN(item[1]);
var data = coinData[date];
if (data != null) {
data.price = price;
} else {
coinData[date] = MarketChartData(date, price: price);
}
}
}
});
// market_caps
workingList = Convert.toListN(map['market_caps']);
workingList?.forEach((e) {
final item = Convert.toListN(e);
if (item != null) {
final date = Convert.toDateTimeN(item[0], unit: TimeUnit.milliseconds);
if (date != null) {
final marketCap = Convert.toDoubleN(item[1]);
var data = coinData[date];
if (data != null) {
data.marketCap = marketCap;
} else {
coinData[date] = MarketChartData(date, marketCap: marketCap);
}
}
}
});
// total_volumes
workingList = Convert.toListN(map['total_volumes']);
workingList?.forEach((e) {
final item = Convert.toListN(e);
if (item != null) {
final date = Convert.toDateTimeN(item[0], unit: TimeUnit.milliseconds);
if (date != null) {
final totalVolume = Convert.toDoubleN(item[1]);
var data = coinData[date];
if (data != null) {
data.totalVolume = totalVolume;
} else {
coinData[date] = MarketChartData(date, totalVolume: totalVolume);
}
}
}
});
return coinData.values.toList(
growable: false,
);
}