0

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

Link of the dart file

Vasav Chaturvedi
  • 97
  • 1
  • 3
  • 8

3 Answers3

4

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" ,
esentis
  • 4,190
  • 2
  • 12
  • 28
3

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.

h8moss
  • 4,626
  • 2
  • 9
  • 25
2

Try changing the extractedData to jsonDecode(response.body)

from

if (extractedData == null)

to

if (jsonDecode(response.body) == null)
Andrew Ryan
  • 1,489
  • 3
  • 15
  • 21