Skip to content

[BUG] Division by Zero Crash in Rating Calculations Affecting Pair Chat, Friends, and User Profiles Issue Body: #814

@Muneerali199

Description

@Muneerali199

Describe the bug

Multiple controllers perform division operations (ratingTotal / ratingCount) without checking if ratingCount is zero. This causes runtime exceptions or produces double.infinity values, leading to crashes or broken UI in critical user flows.

Affected Files & Lines

File Line Code
lib/controllers/pair_chat_controller.dart 83 "userRating": authController.ratingTotal / authController.ratingCount
lib/controllers/friends_controller.dart 69 authStateController.ratingTotal / authStateController.ratingCount
lib/controllers/user_profile_controller.dart 153 userData['ratingTotal'] / userData['ratingCount']
lib/controllers/explore_story_controller.dart 202, 221 userData['ratingTotal'] / userData['ratingCount']

Root Cause

In lib/controllers/auth_state_controller.dart (lines 50-51), the rating fields are declared as late:

late double ratingTotal;
late int ratingCount;
For new users or uninitialized state, ratingCount can be 0, causing division by zero.
Impact
- Pair Chat: Matching fails or shows broken rating UI
- Friends Screen: Friend request cards crash or display ∞ rating
- User Profile: Profile screen crashes when viewing new users
- Story Explore: Story cards show invalid ratings
Steps to Reproduce
1. Create a new user account with no ratings
2. Navigate to Pair Chat screen
3. Observe crash or broken UI
4. Navigate to Friends screen
5. Observe crash or double.infinity displayed
Expected Behavior
- New users should display a default rating (e.g., 5.0 or "No ratings yet")
- Division should be guarded with zero-checks
- UI should gracefully handle uninitialized rating state
Suggested Fix
// In auth_state_controller.dart - initialize with safe defaults
double ratingTotal = 5.0;
int ratingCount = 1;
// At all division sites, add guards:
double userRating = ratingCount > 0 ? ratingTotal / ratingCount : 5.0;
// Or use null-aware pattern:
double? get averageRating => ratingCount > 0 ? ratingTotal / ratingCount : null;
Additional Context
This is a structural hardening issue that affects core user flows. The fix requires:
1. Safe initialization of rating fields
2. Zero-check guards at all 6 division sites
3. UI fallbacks for null/zero rating states

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions