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