Skip to content

fix(nutrition): resolve fl_chart app crash when toggling meal details in Nutritional Plan Detail screen#1202

Merged
rolandgeider merged 1 commit into
wger-project:masterfrom
pankaj-basnet:fix/fl-chart-unsupported-operation-error
May 22, 2026
Merged

fix(nutrition): resolve fl_chart app crash when toggling meal details in Nutritional Plan Detail screen#1202
rolandgeider merged 1 commit into
wger-project:masterfrom
pankaj-basnet:fix/fl-chart-unsupported-operation-error

Conversation

@pankaj-basnet
Copy link
Copy Markdown
Contributor

🧩 The Problem

When a user toggles the meal details view (_toggleDetails in meal.dart) on a meal that has no logged entries today but contains a planned macro goal, the application crashes with an unhandled exception:
Unsupported operation: Infinity or NaN toInt

This occurs because MealDiaryBarChartWidget maps target and actual calorie values straight into percentages to render BarChartRodData. If widget._logged.energy (or any other macro) evaluates to 0, Dart handles the calculation as Infinity. When the underlying fl_chart package attempts to paint the background grid lines and axis intervals, it attempts to convert this infinity value to an integer (~/), resulting in a hard crash on the canvas layer.

🛠️ The Fix

Instead of passing raw mathematical division results directly to the chart data structures, this PR introduces a safePercent helper method inside lib/widgets/nutrition/charts.dart.

The helper safely sanitizes inputs before they ever reach fl_chart:

  • Returns 0.0 immediately if the planned/logged target value is less than or equal to 0.
  • Adds explicit logic guards checking for .isNaN and .isInfinite anomalies to ensure unexpected data states output a clean, non-breaking 0% bar representation.

Related Issue(s)

Test

Added a comprehensive widget test suite (test/nutrition/nutritional_meal_charts_test.dart) to ensure the chart renders safely under the following conditions:

  • Valid data inputs.
  • Logged meal data resolving to zero.
  • Planned macro data resolving to zero.
  • Both logged and planned data resolving to zero simultaneously.

Please check that the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features)
  • Set a 100-character limit to avoid white space diffs (run dart format .)

📱 UI Changes & Verification

1. Empty State (No Meals) 2. Logging a New Meal 3. Meal Details View
Dashboard showing no logged meals empty state Form layout adding a new meal log Detailed breakdown of nutritional info for a meal

…d/planned macros are empty in MealDiaryBarChart

 Bug: Unsupported operation: Infinity or NaN toInt (package:fl_chart)
@pankaj-basnet pankaj-basnet changed the title fix(nutrition): resolve fl_chart crash on division by zero when logge… fix(nutrition): resolve fl_chart app crash when toggling meal details in Nutritional Plan Detail screen May 21, 2026
@rolandgeider rolandgeider merged commit 802fc4d into wger-project:master May 22, 2026
5 checks passed
@rolandgeider
Copy link
Copy Markdown
Member

thanks!

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

Successfully merging this pull request may close these issues.

App crashes when 'meal details' toggles in Nutritional Plan Detail screen if zero planned/logged values

2 participants