Shraddha
Shraddha

Reputation: 65

Error: type 'String' is not a subtype of type 'List<dynamic>' in type cast

I am trying to display chart from Json data.Json data fetched from the local file. I don's understand about this error also I didn't find any related solution for this. How shoul i solve this error Here is my code,

import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;


class HomePage extends StatefulWidget {
  final Widget child;

  HomePage({Key key, this.child}) : super(key: key);

  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<charts.Series<Sales, String>> seriesBarData;

  _generateData() async {
    final decoded = await DefaultAssetBundle.of(context).loadString("asset/data.json") as List;

    seriesBarData.add(charts.Series(
      data:decoded,
      domainFn: (Sales sales, _) => sales.saleyear,
      measureFn: (Sales sales, _) => int.parse(sales.saleval),
      id: 'Performance',
    ));
  }

  @override
  void initState() {
    super.initState();
    seriesBarData = List<charts.Series<Sales, String>>();
    _generateData();
  }

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.cyan,
          title: Text('flutter charts'),
        ),
        body:  Column(
                    children: [
                      Text(
                        'Sales By Year',
                        style: TextStyle(
                            fontSize: 24.0, fontWeight: FontWeight.bold),
                      ),
                      SizedBox(
                        height: 10.0,
                      ),
                      Expanded(
                          child: charts.BarChart(
                            seriesBarData,
                            animate: true,
                            animationDuration: Duration(seconds: 5),
                          ),
                      ),
                    ],
                  ),
                );

  }
}

Here is the model class,

class Sales {
      String saleyear;
      String saleval;


      // Add Constructor
      Sales(this.saleyear, this.saleval);
    }

I am trying to get JSON data from the local file. This is the JSON file,

[

  {
    "saleyear": "2015",
    "saleval": "10"
  },
  {
    "saleyear": "2016",
    "saleval": "30"
  },
  {
    "saleyear": "2017",
    "saleval": "50"
  },
  {
    "saleyear": "2018",
    "saleval": "10"
  }
]

Upvotes: 3

Views: 4647

Answers (3)

Viren V Varasadiya
Viren V Varasadiya

Reputation: 27177

You have to decode json and then you have to create sales object.

i made few change in your code i hope it helps you.

class HomePage extends StatefulWidget {
  final Widget child;

  HomePage({Key key, this.child}) : super(key: key);

  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<charts.Series<Sales, String>> seriesBarData;

  _generateData() async {
    final load =
        await DefaultAssetBundle.of(context).loadString("assets/delete.json");
    var decoded = json.decode(load);
    List<Sales> sales = [];
    for (var item in decoded) {
      sales.add(Sales.fromJson(item));
    }

    seriesBarData.add(charts.Series(
      data: sales,
      domainFn: (Sales sales, _) => sales.saleyear,
      measureFn: (Sales sales, _) => int.parse(sales.saleval),
      id: 'Performance',
    ));
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    seriesBarData = List<charts.Series<Sales, String>>();
    _generateData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.cyan,
        title: Text('flutter charts'),
      ),
      body: Column(
        children: [
          Text(
            'Sales By Year',
            style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),
          ),
          SizedBox(
            height: 10.0,
          ),
          seriesBarData.length > 0
              ? Expanded(
                  child: charts.BarChart(
                    seriesBarData,
                    animate: true,
                    animationDuration: Duration(seconds: 5),
                  ),
                )
              : Container(),
        ],
      ),
    );
  }
}

class Sales {
  String saleyear;
  String saleval;

  Sales(this.saleyear, this.saleval);

  Sales.fromJson(Map<String, dynamic> json) {
    saleyear = json['saleyear'];
    saleval = json['saleval'];
  }
}

Upvotes: 2

AliAzad
AliAzad

Reputation: 263

Did you import the model? I can't see in your code.

Upvotes: 0

Lunedor
Lunedor

Reputation: 1514

Could you try to change this;

final decoded = await DefaultAssetBundle.of(context).loadString("asset/data.json") as List<Sales>;

or this if it didn't work,

Future<Sales> _generateData() async {...

Upvotes: 0

Related Questions