We suspect that you have not assigned the controller value in the onRendererCreated callback like in the below code snippet which is the reason for getting a late initialization error.
LineSeries<_ChartData, int>(
onRendererCreated: (ChartSeriesController controller) {
chartSeriesController = controller;
},
// Other required properties.
)
And we have created the list of ChartSeriesController and updated the list dynamically for each line series so that it will work properly, and we have attached the sample code snippet below for your reference.
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:syncfusion_flutter_core/core.dart';
void main() {
return runApp(ChartApp());
}
class ChartApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Chart Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: LiveLineChart(),
);
}
}
class LiveLineChart extends StatefulWidget {
/// Creates the realtime line chart sample.
const LiveLineChart({Key? key}) : super(key: key);
@override
_LiveLineChartState createState() => _LiveLineChartState();
}
/// State class of the realtime line chart.
class _LiveLineChartState extends State<LiveLineChart> {
_LiveLineChartState() {
timer = Timer.periodic(const Duration(milliseconds: 100), (_timer) {
for (int i = 0; i < 4; i++) {
_updateDataSource(_timer, chartData[i] as List<_ChartData>,
chartSeriesController![i]);
}
});
}
Timer? timer;
int seriesCount = 2;
List<List<dynamic>> chartData = [];
List<_ChartData> chartData1 = [];
List<_ChartData> chartData2 = [];
List<_ChartData> chartData3 = [];
List<_ChartData> chartData4 = [];
late int count;
ChartSeriesController? _chartSeriesController1;
ChartSeriesController? _chartSeriesController2;
ChartSeriesController? _chartSeriesController3;
ChartSeriesController? _chartSeriesController4;
List<dynamic>? chartSeriesController = [];
@override
void dispose() {
timer?.cancel();
chartData[0].clear();
chartData[1].clear();
chartData[2].clear();
chartData[3].clear();
super.dispose();
}
@override
void initState() {
count = 19;
chartSeriesController = <dynamic>[
_chartSeriesController1,
_chartSeriesController2,
_chartSeriesController3,
_chartSeriesController4
];
chartData = <List<dynamic>>[chartData1, chartData2, chartData3, chartData4];
chartData[0] = <_ChartData>[
_ChartData(0, 42),
];
chartData[1] = <_ChartData>[
_ChartData(0, 42),
];
chartData[2] = <_ChartData>[
_ChartData(0, 42),
];
chartData[3] = <_ChartData>[
_ChartData(0, 42),
];
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(appBar: AppBar(), body: _buildLiveLineChart());
}
/// Returns the realtime Cartesian line chart.
SfCartesianChart _buildLiveLineChart() {
return SfCartesianChart(
plotAreaBorderWidth: 0,
primaryXAxis:
NumericAxis(majorGridLines: const MajorGridLines(width: 0)),
primaryYAxis: NumericAxis(
axisLine: const AxisLine(width: 0),
majorTickLines: const MajorTickLines(size: 0)),
series: getLineSeries());
}
List<LineSeries<_ChartData, num>> getLineSeries() {
List<LineSeries<_ChartData, num>> lineSeries = [];
for (int i = 0; i < 4; i++) {
lineSeries.add(LineSeries<_ChartData, int>(
onRendererCreated: (ChartSeriesController controller) {
chartSeriesController![i] = controller;
},
dataSource: chartData[i] as List<_ChartData>,
xValueMapper: (_ChartData sales, _) => sales.country,
yValueMapper: (_ChartData sales, _) => sales.sales,
animationDuration: 0,
));
}
return lineSeries;
}
///Continously updating the data source based on timer
void _updateDataSource(Timer timer, List<_ChartData> chartData,
ChartSeriesController _chartSeriesController) {
chartData.add(_ChartData(count, _getRandomInt(10, 100)));
if (chartData.length == 20) {
chartData.removeAt(0);
_chartSeriesController.updateDataSource(
addedDataIndexes: <int>[chartData.length - 1],
removedDataIndexes: <int>[0],
);
} else {
_chartSeriesController.updateDataSource(
addedDataIndexes: <int>[chartData.length - 1],
);
}
count = count + 1;
}
///Get the random data
int _getRandomInt(int min, int max) {
final Random _random = Random();
return min + _random.nextInt(max - min);
}
}
/// Private calss for storing the chart series data points.
class _ChartData {
_ChartData(this.country, this.sales);
final int country;
final num sales;
}