════════小部件库捕获到异常══════════════════════════════════ ═════════════════════ 在构建Builder(dirty)时引发了以下NoSuchMethodError: 方法'> ='在null上被调用。 接收者:null 尝试致电:> =(25)
相关的引起错误的小部件是: MaterialApp文件:/// C:/Users/Ahmed/AndroidStudioProjects/bmi_calc/lib/main.dart:8:12 引发异常时,这是堆栈:
在#1我的代码是
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';
}
}
}
在#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())));
},)`
答案 0 :(得分:0)
发生问题是因为值_bmi为空。这是因为您没有调用calculatebmi()。
您正在使用Calculator()的空参数来计算BMI。因此,与其设置
bmi:calculator().getresult();
用作:
bmi : cal.getresult(); // You already have insteance of calculator (cal)