Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails on calculate #2

Open
LeeHunter opened this issue Feb 27, 2020 · 12 comments
Open

Fails on calculate #2

LeeHunter opened this issue Feb 27, 2020 · 12 comments

Comments

@LeeHunter
Copy link

Whether I run this code my version or the "completed" version I get the following message after pressing the Calculate button:

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building Builder(dirty):
The method '>=' was called on null.
Receiver: null
Tried calling: >=(25.0)

The relevant error-causing widget was:
MaterialApp file:///Users/leehunter/angelayucourse/BMI-Calculator-Flutter-Completed/lib/main.dart:9:12
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 CalculatorBrain.getResult (package:bmi_calculator/calculator_brain.dart:17:14)
#2 _InputPageState.build.. (package:bmi_calculator/screens/input_page.dart:224:38)
#3 MaterialPageRoute.buildPage (package:flutter/src/material/page.dart:87:27)
#4 _ModalScopeState.build. (package:flutter/src/widgets/routes.dart:710:43)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

@hoctor
Copy link

hoctor commented Feb 27, 2020

You may be declaring _bmi twice, check inside calculateBMI, must be like this

class CalculatorBrain {
CalculatorBrain({this.height, this.weight});

final int height;
final int weight;

double _bmi;

String calculateBMI() {
_bmi = weight / pow(height / 100, 2);
return _bmi.toStringAsFixed(1);
}

@LeeHunter
Copy link
Author

Thanks but that's not it.

@elliotxin
Copy link

@LeeHunter you should initialize the "_bmi" value first sine the flutter update to the new version. must like this: "double _bmi = 0.0;"

@mesutdinler
Copy link

mesutdinler commented Apr 26, 2020

@LeeHunter you should initialize the "_bmi" value first sine the flutter update to the new version. must like this: "double _bmi = 0.0;"

thanks a lot.

@fcrignon
Copy link

hello, I fixed it doing like follow:

I change the a bit this two methods in calculator_brain.dart

String getResult(String bmi) {
_bmi = double.parse(bmi);
...

}

String getInterpretation(String bmi) {
_bmi = double.parse(bmi);
...

}

and I added one line in the onTap property in input_page.dart

onTap: () {
CalculatorBrain calc =CalculatorBrain(height: height, weight: weight);
String bmi =calc.calculateBMI();
Navigator.push(...)
);
},

@mehrheer
Copy link

mehrheer commented Jun 4, 2020

GetResult() method is being called before the calculateBMI() method, so even if you initialize _bmi to be 0.0, your app won't crash anymore but you would still get the wrong output on your screen.

@Mahad61
Copy link

Mahad61 commented Aug 31, 2020

i have the same error then after i check if i change the sequence name like first bmiResult then resultText and then interpretation according to the result_page constructor in input_page navigator my error fixed try it
BottomButton(
label: 'CALCULATE',
onTap: () {
CalculatorBrain calc =
CalculatorBrain(weight: weight, height: height);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ResultsPage(
bmiResult: calc.calculateBMI(),
resultText: calc.getResult(),
interpretation: calc.getInterpretation(),
)));
},
)

@Hoxtygen
Copy link

hello, I fixed it doing like follow:

I change the a bit this two methods in calculator_brain.dart

String getResult(String bmi) {
_bmi = double.parse(bmi);
...

}

String getInterpretation(String bmi) {
_bmi = double.parse(bmi);
...

}

and I added one line in the onTap property in input_page.dart

onTap: () {
CalculatorBrain calc =CalculatorBrain(height: height, weight: weight);
String bmi =calc.calculateBMI();
Navigator.push(...)
);
},

This is what worked for me. Good job man.

@felexkuria
Copy link

thanks @Hoxtygen worked like charm

@fobf
Copy link

fobf commented Sep 18, 2021

class CalcuatorBrain {
  CalcuatorBrain({this.height, this.weight});

  final int? height;
  final int? weight;

  final double _bmi = 0.0;

  String calculateBMI() {
    double _bmi = weight! / pow(height! / 100, 2);
    // bmi != null ? bmi : null; // null check
    return _bmi.toStringAsFixed(1);
  }

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

  String getInterpretation() {
    if (_bmi >= 25) {
      return 'Try to exercise more.';
    } else if (_bmi >= 18.5) {
      return 'Good job,';
    } else {
      return 'You can eat a bit more.';
    }
  }
}

can somb tell me why I am always getting the underweight results? BMI is different but results are the same.

@fobf
Copy link

fobf commented Sep 19, 2021

I found the answer :

import 'dart:math';

class CalcuatorBrain {
  CalcuatorBrain({this.height, this.weight})
      : _bmi = weight! / pow(height! / 100, 2);

  final int? height;
  final int? weight;

  double _bmi;

  String calculateBMI() {
    _bmi = weight! / pow(height! / 100, 2);
    return _bmi.toStringAsFixed(1);
  }

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

  String getInterpretation() {
    if (_bmi >= 25) {
      return 'Try to exercise more.';
    } else if (_bmi >= 18.5) {
      return 'Good job,';
    } else {
      return 'You can eat a bit more.';
    }
  }
}

from here:
https://stackoverflow.com/questions/69245524/dart-flutter-bmi-code-returns-bmi-value-but-other-values-are-wrrong/69246272#69246272

@TechParkBB
Copy link

this works for me in the latest Android Studio version

`class CalculatorBrain {
CalculatorBrain({required this.height, required this.weight});
final int height;
final int weight;
double _bmi = 0.0;

String calculateBMI() {
_bmi = weight / pow(height / 100, 2);
return _bmi.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

No branches or pull requests