3

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"
  }
]

3 Answers 3

2

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'];
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your Welcome!.. don't forgot to mark my answer as correct. @Shraddha
0

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 {...

1 Comment

Thank you. I have tried both but not working. It gives an same error.
0

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

2 Comments

do you mean model class?
Yes. the model class.

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.