0

I am not getting why / operator is giving me this error. plz someone help me with it FYI i am making a BMI calculator app with refernce to Angela Yu's course on Udemy.

import 'dart:math';

class CalculatorBrain {
  CalculatorBrain({this.height, this.weight});
  final int? height;
  final int? weight;
  double _bmi = 0;
  String calculateBMI() {
    _bmi = weight / pow(height / 100, 2);
    return _bmi.toStringAsFixed(1);
  }

  String getResults() {
    if (_bmi >= 25)
      return 'Overweight';
    else if (_bmi > 18.5)
      return 'Normal';
    else
      return 'Underweight';
  }

  String getInterpretation() {
    if (_bmi >= 25)
      return 'You have a higher than normal body weigth. Try to exercise more.';
    else if (_bmi > 18.5)
      return 'You have a normal body weight. Good job!';
    else
      return 'You have a lower than normal body weight. You can eat a bit more.';
  }
}

your efforts will be appreciated

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • `weight` in this line `_bmi = weight / pow(height / 100, 2);` of your code can be null and that's why you are getting this error. – ibhavikmakwana Feb 15 '22 at 17:49
  • you can learn more about null safety here. https://dart.dev/null-safety – ibhavikmakwana Feb 15 '22 at 17:50
  • @ibhavikmakwana sir i had tried if(weight!=null) { _bmi = weight / pow(height / 100, 2); } but it won't even work – Rohan Patil Feb 15 '22 at 17:53
  • Does this answer your question? [The following NoSuchMethodError was thrown building Builder(dirty): The method '>=' was called on null. Receiver: null The relevant error-causing was:](https://stackoverflow.com/questions/67455190/the-following-nosuchmethoderror-was-thrown-building-builderdirty-the-method) – nvoigt Feb 15 '22 at 18:41
  • You guys should really get a better course, this class design is horrible. And I don't believe [every](https://stackoverflow.com/questions/66617625/how-to-fix-the-method-was-called-on-null-receiver-null-tried-calling/66617799#66617799) [single](https://stackoverflow.com/questions/70844939/null-safety-in-flutter-and-dart) [one](https://stackoverflow.com/questions/67455190/the-following-nosuchmethoderror-was-thrown-building-builderdirty-the-method/67455337#67455337) of you magically came up with the same code by accident. – nvoigt Feb 15 '22 at 18:44

2 Answers2

1

You have to find a good fallback in case height or wight is null. One way to do this is using the ?? operator.

This is how you can fallback to 0 in both cases, which would cause more errors because you then would divide by 0.

_bmi = (weight ?? 0) / pow((height ?? 0) / 100, 2);

Better, set _bmi to a specific value when one or both values are missing.

  double _bmi = 0;
  String calculateBMI() {
    // always set it to 0 first in case values are missing
    _bmi = 0;
    if (height != null && weight != null) {
      _bmi = weight! / pow(height! / 100, 2);
    }
    return _bmi.toStringAsFixed(1);
  }
passsy
  • 5,162
  • 4
  • 39
  • 65
0

The answer by @passsy is technically right but I'm wondering why you declared height and weight as nullable. You could rewrite your class as below by making the 2 parameters required (and thus making sure they cannot be null):

class CalculatorBrain {
  CalculatorBrain({required this.height, required this.weight});
  final int height;
  final int weight;
  double _bmi = 0;
  String calculateBMI() {
    _bmi = weight / pow(height / 100, 2);
    return _bmi.toStringAsFixed(1);
  }
passsy
  • 5,162
  • 4
  • 39
  • 65
ManuH68
  • 481
  • 2
  • 6