1

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?

1
  • Try: data[x] = equity.elementAt(i).toDouble(). This method is specified for num and can therefore be called on both int and double to get a double value. Commented Jan 9, 2021 at 20:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.