Skip to content

Commit f6705ec

Browse files
committed
changed the sample
1 parent 8c91013 commit f6705ec

File tree

1 file changed

+49
-18
lines changed

1 file changed

+49
-18
lines changed

lib/main.dart

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,15 @@ class _MyHomePageState extends State<MyHomePage> {
3131

3232
Future loadSalesData() async {
3333
String jsonString = await rootBundle.loadString('assets/data.json');
34-
final jsonResponse = json.decode(jsonString);
35-
setState(() {
36-
for (Map<String, dynamic> i in jsonResponse) {
37-
chartData.add(SalesData.fromJson(i));
38-
}
39-
});
34+
final dynamic jsonResponse = json.decode(jsonString);
35+
for (Map<String, dynamic> i in jsonResponse) {
36+
chartData.add(SalesData.fromJson(i));
37+
}
4038
}
4139

4240
@override
4341
void initState() {
4442
super.initState();
45-
loadSalesData();
4643
}
4744

4845
@override
@@ -51,17 +48,51 @@ class _MyHomePageState extends State<MyHomePage> {
5148
appBar: AppBar(
5249
title: const Text('Syncfusion Flutter chart'),
5350
),
54-
body: SfCartesianChart(
55-
primaryXAxis: CategoryAxis(),
56-
// Chart title
57-
title: ChartTitle(text: 'Half yearly sales analysis'),
58-
series: <ChartSeries<SalesData, String>>[
59-
LineSeries<SalesData, String>(
60-
dataSource: chartData,
61-
xValueMapper: (SalesData sales, _) => sales.month,
62-
yValueMapper: (SalesData sales, _) => sales.sales,
63-
)
64-
]));
51+
body: Center(
52+
child: FutureBuilder(
53+
future: loadSalesData(),
54+
builder: (context, snapshot) {
55+
if (chartData.isNotEmpty) {
56+
return SfCartesianChart(
57+
primaryXAxis: CategoryAxis(),
58+
// Chart title
59+
title: ChartTitle(text: 'Half yearly sales analysis'),
60+
series: <ChartSeries<SalesData, String>>[
61+
LineSeries<SalesData, String>(
62+
dataSource: chartData,
63+
xValueMapper: (SalesData sales, _) => sales.month,
64+
yValueMapper: (SalesData sales, _) => sales.sales,
65+
)
66+
]);
67+
} else {
68+
return Card(
69+
elevation: 5.0,
70+
child: Container(
71+
height: 100,
72+
width: 400,
73+
child: Center(
74+
child: Row(
75+
mainAxisAlignment: MainAxisAlignment.spaceAround,
76+
children: [
77+
Text('Retriving JSON data...',
78+
style: TextStyle(fontSize: 20.0)),
79+
Container(
80+
height: 40,
81+
width: 40,
82+
child: CircularProgressIndicator(
83+
semanticsLabel: 'Retriving JSON data',
84+
valueColor: AlwaysStoppedAnimation<Color>(
85+
Colors.blueAccent),
86+
backgroundColor: Colors.grey[300],
87+
),
88+
),
89+
],
90+
),
91+
),
92+
),
93+
);
94+
}
95+
})));
6596
}
6697
}
6798

0 commit comments

Comments
 (0)