diff --git a/lib/src/chart/line_chart/line_chart_painter.dart b/lib/src/chart/line_chart/line_chart_painter.dart index 3932f6d3c..6b10bb51b 100644 --- a/lib/src/chart/line_chart/line_chart_painter.dart +++ b/lib/src/chart/line_chart/line_chart_painter.dart @@ -1385,7 +1385,7 @@ class LineChartPainter extends AxisChartPainter { } /// Find the nearest spot (based on distanceCalculator) - final sortedSpots = []; + FlSpot? nearestSpot; double? smallestDistance; for (final spot in barData.spots) { if (spot.isNull()) continue; @@ -1398,22 +1398,24 @@ class LineChartPainter extends AxisChartPainter { ); if (distance <= data.lineTouchData.touchSpotThreshold) { - smallestDistance ??= distance; + if (nearestSpot == null) { + nearestSpot = spot; + smallestDistance = distance; + continue; + } - if (distance < smallestDistance) { - sortedSpots.insert(0, spot); + if (distance < smallestDistance!) { + nearestSpot = spot; smallestDistance = distance; - } else { - sortedSpots.add(spot); } } } - if (sortedSpots.isNotEmpty) { + if (nearestSpot != null) { return TouchLineBarSpot( barData, barDataPosition, - sortedSpots.first, + nearestSpot, smallestDistance!, ); } else {