0
votes

I need to pass 2 arguments in ChartSampleData() but I'm having trouble because it says that

List<ChartSampleData> _list = [];
_list.add(ChartSampleData.fromMap(
                '${formattedDate.toString()}', redeemedToday[index].total));

err

the argument type 'String' can't be assigned to the parameter type 'Map<String,dynamic>'

here's my class

i tried doing these,

Code:

class ChartSampleData {
  ChartSampleData(this.xValue, this.yValue);
  ChartSampleData.fromMap(
      Map<String, dynamic> dataMap0, Map<String, dynamic> dataMap1)
      : xValue = dataMap0['x'],
        yValue = dataMap1['y'];

  final dynamic xValue;
  final dynamic yValue;
}
2
ChartSampleData.fromMap specifies that it needs a Map<String, dynamic> as an input and you are giving it '${formattedDate.toString()}' which is a string.Abion47

2 Answers

1
votes

I think you need to change your model class like this:

class ChartSampleData {
  String xValue;
  String yValue;

  ChartSampleData({this.xValue, this.yValue});

  ChartSampleData.fromJson(Map<String, dynamic> json) {
    xValue = json['xValue'];
    yValue = json['yValue'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['xValue'] = this.xValue;
    data['yValue'] = this.yValue;
    return data;
  }
}

You can use this website to convert your JSON to a Dart Model.

2
votes

You're using the wrong constructor. Use ChartSampleData instead of ChartSampleData.fromMap.

List<ChartSampleData> _list = [];
_list.add(ChartSampleData('${formattedDate.toString()}', redeemedToday[index].total));