════════ Exception caught by widgets library ═══════════════════════════════════════════════════════ The following NoSuchMethodError was thrown building Builder(dirty): The method '>=' was called on null. Receiver: null Tried calling: >=(25)
The relevant error-causing widget was: MaterialApp file:///C:/Users/Ahmed/AndroidStudioProjects/bmi_calc/lib/main.dart:8:12 When the exception was thrown, this was the stack:
At #1 my code is
class calculator {
calculator({this.height, this.weight});
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';
}
return ' ';
}
String getRemarks() {
if (_bmi >= 25) {
return 'Your weight is more than average body weight, try to excercise.';
} else if (_bmi > 18.5) {
return 'Your weight is normal';
} else {
return 'Your weight is less than average body weight, try to eat more';
}
}
}
At #2
`GestureDetector(
onTap: () {
calculator cal = calculator(height: height, weight: weight);
Navigator.push(context, MaterialPageRoute(builder: (context) => results(result: calculator().getresult(), bmi: calculator().calculatebmi(), remarks: calculator().getRemarks())));
},)`
The problem occurs because the value _bmi is null. And it is because you are not calling calculatebmi().
You are calculating BMI with empty parameters of calculator(). So, instead of setting
bmi:calculator().getresult();
use as:
bmi : cal.getresult(); // You already have insteance of calculator (cal)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.