Every time I run my app. It shows
"Null check operator used on a null value".
I have shown some solutions but couldn't successfully apply them to my code. How do I solve this?
import 'dart:math';
class bmicalculator {
bmicalculator({required this.height, required this.weight});
int height;
double weight;
double? bmi;
String Calculation() {
bmi = weight / pow(height / 100, 2);
return bmi!.toStringAsFixed(2);
}
String getResult() {
if (bmi! >= 25) {
return 'Overweight';
} else if (bmi! > 18.5) {
return 'Normal';
} else
return 'UnderWeight';
}
String getInterpretation() {
if (bmi! >= 25) {
return 'String 1';
} else if (bmi! > 18.5) {
return 'String 2';
} else
return 'String 3';
}
}
this would happen when you call
getResultorgetInterpretationwithout calling Calculation previously, and the problem is that you are not initilizing the value ofbmiwhich is a String? type and it's initial value isnull, so when you try to use one of the methodsgetResultorgetInterpretationyou would get error becausebmiisnull,you can solve the issue by initializing the bmi value in the constructor like: