Here is my code:
class PortfolioHistory {
List<int> timestamp;
List<dynamic> equity;
String timeframe;
PortfolioHistory(
{this.timestamp,
this.equity,
this.timeframe});
factory PortfolioHistory.fromJson(final json) {
return new PortfolioHistory(
equity: json['equity'].cast<double>(),
timestamp: json['timestamp'].cast<int>(),
timeframe: json['timeframe'],
);
}
Map<DateTime, double> createCoordinates() {
Map<DateTime, double> data = {};
if (equity != null) {
int length = equity.length;
for(int i = 0; i < length; i++) {
int milliEpoch = timestamp.elementAt(i);
// coordinates
DateTime x = DateTime.fromMillisecondsSinceEpoch(milliEpoch * 1000);
data[x] = equity.elementAt(i);
}
}
return data;
}
}
Apparently this line data[x] = equity.elementAt(i); produces this error:
type 'int' is not a subtype of type 'double' in type cast
I declare my equity as a dynamic list as it takes data from a realtime database and sometimes, some elements of the list will be 'null'. Can anyone please help me?
data[x] = equity.elementAt(i).toDouble(). This method is specified fornumand can therefore be called on bothintanddoubleto get adoublevalue.