I am rendering a chart with different chart types that can be choosed and I use coin gecko as the API. there are for types named Line, Area, Ohlc, and Candle. they work totally fine but when I switch from line or Area to Ohlc to Candle, then this_error shows for a second and then it disappears and shows the chart totally fine!!! I can't understand what the problem is. can you help me please? I am using syncfusion library for showing charts. here is the code:
class ChartAl extends StatefulWidget {
String id;
String chartType;
ChartAl(this.id, this.chartType, {Key? key}) : super(key: key);
@override
_ChartPageState createState() => _ChartPageState();
}
class _ChartPageState extends State<ChartAl> {
@override
Widget build(BuildContext context) {
final bloc = LatestNewsProvider.of(context);
if (widget.id != 'null') {
bloc.chartGecko(widget.id);
bloc.candleGecko(widget.id);
}
if (widget.id == 'null') {
return CircularProgressIndicator(
color: Pallete.dark_primary_color,
);
}
if (widget.chartType == 'Area' || widget.chartType == 'Line') {
return StreamBuilder(
stream: bloc.chartGeckoStream,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
List pricesOfTrades = [];
var xa = [];
var ya = [];
late List<ChartData> chartData;
chartData = [];
for (var i = 0; i < snapshot.data.length; i++) {
xa.add(readTimestamp((snapshot.data[i][0])));
ya.add((snapshot.data[i][1]));
chartData.add(ChartData(xa[i], ya[i]));
}
List<PlotBand> bands = [];
for (var item in pricesOfTrades) {
bands.add(PlotBand(
isVisible: true,
borderWidth: 2,
start: item,
end: item,
borderColor: Colors.green));
}
if (widget.chartType == 'Line') {
return SfCartesianChart(
// primaryYAxis: NumericAxis(
// plotBands: widget.user != 'not' ? bands : null,
// ),
primaryXAxis: DateTimeAxis(
dateFormat: DateFormat.yMMMd(),
intervalType: DateTimeIntervalType.days,
visibleMinimum: chartData[chartData.length - 29].x,
visibleMaximum: chartData[chartData.length - 1].x),
zoomPanBehavior: ZoomPanBehavior(
enablePanning: true,
enablePinching: true,
),
series: <CartesianSeries<ChartData, DateTime>>[
LineSeries(
dataSource: chartData,
xValueMapper: (ChartData tendencias, _) => tendencias.x,
yValueMapper: (ChartData tendencias, _) => tendencias.y,
)
],
trackballBehavior: TrackballBehavior(
enable: true,
lineType: TrackballLineType.horizontal,
tooltipSettings: InteractiveTooltip(
enable: true, color: Pallete.dark_primary_color)),
);
} else if (widget.chartType == 'Area') {
return SfCartesianChart(
// primaryYAxis: NumericAxis(plotBands: []),
primaryXAxis: DateTimeAxis(
dateFormat: DateFormat.yMMMd(),
intervalType: DateTimeIntervalType.days,
visibleMinimum: chartData[chartData.length - 29].x,
visibleMaximum: chartData[chartData.length - 1].x),
zoomPanBehavior: ZoomPanBehavior(
enablePanning: true,
enablePinching: true,
),
series: <CartesianSeries<ChartData, DateTime>>[
AreaSeries(
dataSource: chartData,
xValueMapper: (ChartData tendencias, _) => tendencias.x,
yValueMapper: (ChartData tendencias, _) => tendencias.y,
)
],
trackballBehavior: TrackballBehavior(
enable: true,
lineType: TrackballLineType.horizontal,
tooltipSettings: InteractiveTooltip(
enable: true, color: Pallete.dark_primary_color)),
);
}
}
return Center(
child: CircularProgressIndicator(
color: Pallete.dark_primary_color,
));
},
);
}
return StreamBuilder(
stream: bloc.candleGeckoStream,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
late List<DateTime> xs = [];
late List<double> opens = [];
late List<double> highs = [];
late List<double> lows = [];
late List<double> closes = [];
late List<CandleData> chartData = [];
// print(snapshot.data.length);
for (var i = 0; i < snapshot.data.length; i++) {
xs.add(readTimestamp((snapshot.data[i][0])));
opens.add((snapshot.data[i][1]));
highs.add((snapshot.data[i][2]));
lows.add((snapshot.data[i][3]));
closes.add((snapshot.data[i][4]));
chartData
.add(CandleData(xs[i], opens[i], highs[i], lows[i], closes[i]));
}
if (widget.chartType == 'Ohlc') {
return SfCartesianChart(
// primaryYAxis: NumericAxis(plotBands: []),
primaryXAxis: DateTimeAxis(
dateFormat: DateFormat.yMMMd(),
intervalType: DateTimeIntervalType.days,
visibleMinimum: chartData[chartData.length - 29].x,
visibleMaximum: chartData[chartData.length - 1].x),
zoomPanBehavior: ZoomPanBehavior(
enablePanning: true,
enablePinching: true,
),
series: [
HiloOpenCloseSeries(
dataSource: chartData,
xValueMapper: (CandleData tendencias, _) => tendencias.x,
lowValueMapper: (CandleData tendencias, _) => tendencias.low,
highValueMapper: (CandleData tendencias, _) =>
tendencias.high,
openValueMapper: (CandleData tendencias, _) =>
tendencias.open,
closeValueMapper: (CandleData tendencias, _) =>
tendencias.close,
)
],
trackballBehavior: TrackballBehavior(
enable: true,
lineType: TrackballLineType.horizontal,
tooltipSettings: InteractiveTooltip(
enable: true, color: Pallete.dark_primary_color)),
);
} else if (widget.chartType == 'Candle') {
return SfCartesianChart(
// primaryYAxis: NumericAxis(plotBands: []),
primaryXAxis: DateTimeAxis(
dateFormat: DateFormat.yMMMd(),
intervalType: DateTimeIntervalType.days,
visibleMinimum: chartData[chartData.length - 29].x,
visibleMaximum: chartData[chartData.length - 1].x),
zoomPanBehavior: ZoomPanBehavior(
enablePanning: true,
enablePinching: true,
),
series: [
CandleSeries(
enableSolidCandles: true,
dataSource: chartData,
xValueMapper: (CandleData tendencias, _) => tendencias.x,
lowValueMapper: (CandleData tendencias, _) => tendencias.low,
highValueMapper: (CandleData tendencias, _) =>
tendencias.high,
openValueMapper: (CandleData tendencias, _) =>
tendencias.open,
closeValueMapper: (CandleData tendencias, _) =>
tendencias.close,
)
],
trackballBehavior: TrackballBehavior(
enable: true,
lineType: TrackballLineType.horizontal,
tooltipSettings: InteractiveTooltip(
enable: true, color: Pallete.dark_primary_color)),
);
}
}
return Center(
child: CircularProgressIndicator(
color: Pallete.dark_primary_color,
));
},
);
}
}
class ChartData {
final DateTime x;
final double y;
ChartData(this.x, this.y);
}
class CandleData {
final DateTime x;
final double open;
final double high;
final double low;
final double close;
CandleData(this.x, this.open, this.high, this.low, this.close);
}
readTimestamp(int timestamp) {
var date = DateTime.fromMillisecondsSinceEpoch(timestamp);
return date;
}
As it is shown in the code, I do not have a listview.builder to set the length value for it ! the only thing rendering the data is from syncfusion!
So what I guessed was that this error comes from that library but the strange thing is that it just shows for a second and then it shows the chart ok!
1 - it only happens when I switch from line or area charts to ohlc or canlde and the weird thing is that if I switch so fast without the chart fully loaded it will not show the error !
https://github.com/benyawmin/FlutterCryptocurrencyApp
this is the github link of the project you can totally replicate the problem with git clone command and running the full project.
1 - it only happens when I switch from line or area charts to ohlc or canlde and the weird thing is that if I switch so fast without the chart fully loaded it will not show the error !
https://github.com/benyawmin/FlutterCryptocurrencyApp
this is the github link of the project you can totally replicate the problem with git clone command and running the full project.