Skip to content

Division by zero #799

Description

@mahmoudr80

🐛 Describe the bug

The application crashes when calculating the average rating due to a division by zero error. This occurs when ratingCount is 0, which leads to an invalid division operation.

Steps to Reproduce
Ensure ratingCount is 0
Trigger any code path that calculates the average rating
The app crashes immediately

double ratingTotal = 0;
int ratingCount = 0;

double averageRating = ratingTotal / ratingCount; // ❌ Crash here
print(averageRating);

Unsupported operation: Infinity or NaN toInt

#0 double.toInt (dart:core-patch/double.dart:xxx)
#1 YourWidget.build (package:your_app/your_widget.dart:xx)
#2 StatelessElement.build (package:flutter/src/widgets/framework.dart:xxx)
#3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:xxx)
Expected Behavior

The app should handle cases where ratingCount == 0 gracefully, without crashing.

💡 Suggested Fix

Add a guard condition before division:

double averageRating = ratingCount == 0
? 0
: ratingTotal / ratingCount;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions