flutter - 我的应用程序 throws me 方法在 null 上调用
问题描述
我制作了一个应用程序并创建了一个简单的逻辑。但是当我运行它时它会抛出我
The method '>' was called on null.
Receiver: null
Tried calling: >(25)
我的意思是基本的数学比较,我不知道为什么会这样,这是我的文件
import 'dart:math';
class CalculatorBrain {
CalculatorBrain({this.height, this.weight});
final int height;
final int weight;
double _bmi;
String calculateBMI() {
double _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 'You have a higher than normal body weight. Try to exercise more';
} else if (_bmi > 18.5) {
return 'You have a normal body weight. Good job!';
} else {
return 'You have aa lower than normal body weight. You can eat a bit more.';
}
}
}
你能帮我理解这个错误吗?
解决方案
可能您正在调用getResult()
或getInterpretation()
在将值分配给_bmi
.
为了防止这种情况,您可能需要在比较之前检查是否_bmi
是。null
这是您的getResult
功能的示例:
String getResult() {
if (_ bmi != null){
if (_bmi >= 25) {
return 'Overweight';
} else if (_bmi > 18.5) {
return 'Normal';
} else {
return 'Underweight';
}
}
}
推荐阅读
- r - R dopar foreach 块而不是每行
- angular - Angular-Testing:如何使用 TestBed 为服务类提供配置对象?
- javascript - JQuery:“addClass 不能按 id 工作”,当文档准备好时
- php - laravel 应用程序中的文件更改未反映在网页上
- c# - 如果匹配特定搜索词,则替换数组中的值 - O(n^2)
- g++ - 如何使用 Mingw 链接 .la 文件
- c# - Xunit.Net 由于 ID 重复而跳过测试
- c# - Visual 2019 Preview 是否存在兼容性问题?
- reactjs - 是否可以自定义反应构建(web-pack 构建)?
- javascript - 无法使用 jquery 访问 ajax 加载数据中的表单数据