I have made a BMI calculator in flutter. I am new in flutter. After running the code i am receiving the above warning. Kindly provide me the solution of this problem. Below attached is the link of the code
-
2Please when posting code, copy paste the code into a code block, don't link it outside, it helps us help you – h8moss Dec 20 '21 at 10:16
-
Maybe this helps: https://stackoverflow.com/a/68321491/10740241. The problem seems to be 'late' – twothreezarsix Dec 20 '21 at 10:18
-
Yeah it did help. Thanks – Vasav Chaturvedi Dec 20 '21 at 10:40
3 Answers
You are declaring the _result variable as non-nullable,
late double _result;
However you do a null check
null == _result ? "Enter Value" : _result.toStringAsFixed(2),
So change your variable declaration to this,
double? _result;
And then you can null check this way
_result?.toStringAsFixed(2) ?? "Enter Value" ,
- 4,190
- 2
- 12
- 28
You declared the result variable like this:
late double _result;
_result is the name of the variable, double is the type.
late means you are not yet assigning a value to it.
A late variable is different from a nullable variable in the sense that a nullable variable's value could be null, a late variable doesn't have a value until you assign one.
late int xd;
if (xd == null) {
}
the above if will always be false because xd's value is not null, xd has no value at all.
a nullable variable is determined with a ? after the type:
double? _result;
which means that if you haven't assigned anything to it, it will be null, but you can also assign null directly.
_result = null;
the above is not possible for a late variable.
- 4,626
- 2
- 9
- 25
Try changing the extractedData to jsonDecode(response.body)
from
if (extractedData == null)
to
if (jsonDecode(response.body) == null)
- 1,489
- 3
- 15
- 21
- 61
- 1