Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bad value exception in sumValue with pieChart #1859

Open
g-regoz opened this issue Feb 8, 2025 · 0 comments
Open

bad value exception in sumValue with pieChart #1859

g-regoz opened this issue Feb 8, 2025 · 0 comments

Comments

@g-regoz
Copy link

g-regoz commented Feb 8, 2025

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant