bad value exception in sumValue with pieChart
- Dominant language
- Dart
- Stars
- 7.6k
- Forks
- 2k
- Avg merge
- 9d 1h
- Merged PRs (30d)
- 2
Description
**Describe the bug**
within a pieChart, when using:
pieTouchData: PieTouchData(
touchCallback: (FlTouchEvent event, pieTouchResponse) {
precursorCtl.checkTouchedIndex(event, pieTouchResponse);
},
If the pie section data/value are null/don't exist, it will throw an exception complaining about bad values.
replaced existing sumValue() in pie_chart_data.dart with this (very cautious) code, and seems to now work:
double get sumValue {
// Map the values
var values = sections.map((data) => data.value).toList();
// Print out the values to help with debugging
// print("Mapped values: $values");
// Handle case when list is empty or all values are null
if (values.isEmpty || values.every((v) => v == null)) {
// print("No valid values to sum, returning 0.0");
return 0.0;
}
// Filter out any null values just in case
var nonNullValues = values.where((value) => value != null).toList();
// Print non-null values to help debugging
// print("Non-null values: $nonNullValues");
// Check again for empty list after filtering out nulls
if (nonNullValues.isEmpty) {
// print("No non-null values, returning 0.0");
return 0.0;
}
// Safely sum the values using fold instead of reduce
return nonNullValues.fold(0.0, (sum, value) => sum + value!);
}
**Screenshots**
If applicable, add screenshots, or videoshots to help explain your problem.
**Versions**
- Flutter 3.19.6
- flCharts version 0.67
Contributor guide
Research direction
Start in pie_chart_data.dart at the sumValue getter and trace how PieTouchData and pieTouchResponse reach it when pie section values are null or missing. Reproduce the pieChart case described in the issue, then verify that empty or null-valued sections no longer raise the bad value exception while normal values still sum correctly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart, flutter
- Domain
- data-visualization, frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100