Boby
Boby

Reputation: 1202

Cannot show data to chart - 'List' is not a subtype of type 'Map'

I'm trying to showing chart from json data , i'm using charts_flutter: ^0.7.0 .

Here is my json

[
    {
        "tanggal": 1,
        "total": 1677765
    },
    {
        "tanggal": 2,
        "total": 0
    },
    {
        "tanggal": 3,
        "total": 779920
    },
]

here is what i do

FutureBuilder(
                        future: _getData(),
                        builder: (BuildContext context,AsyncSnapshot snapshot){
                          if (snapshot.connectionState == ConnectionState.waiting &&
                              snapshot.hasError == false) {
                            return Center(child: CircularProgressIndicator(),);
                          } another condition
                           else {
                            return new charts.PieChart(
                                dataList(snapshot.data),
                                defaultRenderer: new charts.ArcRendererConfig(
                                    arcRendererDecorators: [new charts.ArcLabelDecorator()])
                            );
                          }

                        }
                    ),

then here is _getData() and where i try to build my chart

_getData(String tahun, String bulan, String Outlet) async{
    final response = await http.get(Configuration.url + "api/cartData/"+tahun+"/"+bulan+"/"+Outlet);
    print(Configuration.url + "api/cartData/"+tahun+"/"+bulan+"/"+Outlet);
    Map<dynamic,dynamic> map = json.decode(response.body);
    return map;
  }

  static List<charts.Series<LinearSales, int>> dataList(Map<dynamic, dynamic> apiData) {
    List<LinearSales> list = new List();
    for(int i=0; i<apiData.length; i++)
      list.add(new LinearSales(i, apiData[i]));
    return [
      new charts.Series<LinearSales, int>(
        id: 'Sales',
        domainFn: (LinearSales sales, _) => sales.day,
        measureFn: (LinearSales sales, _) => sales.sales,
        data: list,
        labelAccessorFn: (LinearSales row, _) => '${row.day}: ${row.sales}',
      )
    ];
  }

But when i run it i get this

type 'List' is not a subtype of type 'Map'

How can i fix it ? did i miss something ?

Upvotes: 2

Views: 700

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80904

Change this:

 Map<dynamic,dynamic> map = json.decode(response.body);

into this:

 Iterable data = json.decode(response.body);
 List<dynamic> list = data.toList();

Then change this:

  static List<charts.Series<LinearSales, int>> dataList(Map<dynamic, dynamic> apiData) {

into this:

  static List<charts.Series<LinearSales, int>> dataList(List<dynamic> apiData) {

Upvotes: 3

Related Questions