Skip to content

fix Division by zero bug#800

Open
mahmoudr80 wants to merge 1 commit intoAOSSIE-Org:masterfrom
mahmoudr80:fix_Division_by_zero
Open

fix Division by zero bug#800
mahmoudr80 wants to merge 1 commit intoAOSSIE-Org:masterfrom
mahmoudr80:fix_Division_by_zero

Conversation

@mahmoudr80
Copy link
Copy Markdown

@mahmoudr80 mahmoudr80 commented Mar 26, 2026

Description

This PR fixes a crash in the Profile screen caused by a division by zero when calculating the user's rating.

Fixes # (issue)

Added a guard condition to prevent division when ratingCount == 0
Displays a default rating (0.0) when no ratings exist

Type of change

Previously:

(authController.ratingTotal / authController.ratingCount)

This caused a runtime crash when ratingCount was 0.

Solution
(authController.ratingCount == 0)
? "0.0"
: (authController.ratingTotal / authController.ratingCount).toStringAsFixed(1)

Please delete options that are not relevant.

  • [o] Bug fix (non-breaking CHANGE which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (does not change functionality, e.g. code style improvements, linting)
  • Documentation update

How Has This Been Tested?

Manual Testing
Set ratingCount = 0
Open Profile screen
Verified:
No crash occurs
Rating displays as 0.0
Set ratingCount > 0
Verified:
Rating displays correctly with 1 decimal

Please include screenshots below if applicable.

Checklist:

  • [o] My code follows the style guidelines of this project
  • [o] I have performed a self-review of my own code
  • [o] I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • [o] My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • [o] New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • [o] I have checked my code and corrected any misspellings

Maintainer Checklist

x e labels

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced rating display logic to accurately calculate and show the average rating when available, with proper fallback handling for instances with no ratings.

@mahmoudr80 mahmoudr80 requested a review from M4dhav as a code owner March 26, 2026 08:02
@github-actions
Copy link
Copy Markdown
Contributor

🎉 Welcome @mahmoudr80!
Thank you for your pull request! Our team will review it soon. 🔍

  • Please ensure your PR follows the contribution guidelines. ✅
  • All automated tests should pass before merging. 🔄
  • If this PR fixes an issue, link it in the description. 🔗

We appreciate your contribution! 🚀

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 26, 2026

📝 Walkthrough

Walkthrough

A guard condition was added to the rating display logic in the profile screen to prevent division by zero when ratingCount equals zero. The calculation now checks if ratingCount is non-zero before dividing, otherwise displaying "0.0" as the fallback value.

Changes

Cohort / File(s) Summary
Division by Zero Guard
lib/views/screens/profile_screen.dart
Added conditional check in rating display logic to prevent division by zero crash. Shows calculated average rating (ratingTotal / ratingCount) when count is non-zero; otherwise displays "0.0".

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A fraction so simple, yet oh-so-mean,
Zero below made the app scream!
Now we check before we divide,
Zero-safe ratings run with pride ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'fix Division by zero bug' accurately describes the main change addressing the division by zero crash in rating calculation.
Linked Issues check ✅ Passed The code changes directly address issue #799 by adding a guard condition checking ratingCount != 0 before division and displaying '0.0' when no ratings exist, matching the suggested fix.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the division by zero bug in the rating display logic; no unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@lib/views/screens/profile_screen.dart`:
- Around line 206-213: The creator-profile rating can be null and is being
force-formatted in the Text widget; update the display logic in
profile_screen.dart (where widget.isCreatorProfile and
widget.creator!.userRating are used) to fall back to "0.0" when
widget.creator!.userRating is null or when ratingCount is zero, and/or normalize
the value when constructing ResonateUser so userRating is non-null (compute
ratingTotal/ratingCount only if ratingCount>0 else set 0.0) in the ResonateUser
creation path (see explore_story_controller where ratingTotal/ratingCount are
computed) so the UI can safely call toStringAsFixed(1).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ca3e114d-5037-42a6-b0df-44d2c9dd3d5f

📥 Commits

Reviewing files that changed from the base of the PR and between bf1dbe2 and 260f271.

📒 Files selected for processing (1)
  • lib/views/screens/profile_screen.dart

Comment on lines +206 to +213
child:Text(
widget.isCreatorProfile == null
? (authController.ratingTotal /
? authController.ratingCount!=0?
(authController.ratingTotal /
authController.ratingCount)
.toStringAsFixed(1)
: widget.creator!.userRating!.toStringAsFixed(1),
),
: "0.0":widget.creator!.userRating!.toStringAsFixed(1),
),),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

The zero-rating fix doesn't cover creator profiles.

Line 212 still force-formats widget.creator!.userRating!. That field is nullable in lib/models/resonate_user.dart:16, and lib/controllers/explore_story_controller.dart:201-202 still computes it from ratingTotal / ratingCount without a zero-count guard. Creator profiles with no ratings can therefore still end up with an invalid value here. Please apply the same 0.0 fallback there, or normalize userRating before constructing ResonateUser.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/views/screens/profile_screen.dart` around lines 206 - 213, The
creator-profile rating can be null and is being force-formatted in the Text
widget; update the display logic in profile_screen.dart (where
widget.isCreatorProfile and widget.creator!.userRating are used) to fall back to
"0.0" when widget.creator!.userRating is null or when ratingCount is zero,
and/or normalize the value when constructing ResonateUser so userRating is
non-null (compute ratingTotal/ratingCount only if ratingCount>0 else set 0.0) in
the ResonateUser creation path (see explore_story_controller where
ratingTotal/ratingCount are computed) so the UI can safely call
toStringAsFixed(1).

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.

Division by zero

1 participant