Home > Enterprise >  The operand can't be null, so the condition is always false. I am receiving this warning
The operand can't be null, so the condition is always false. I am receiving this warning

Time:12-21

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

CodePudding user response:

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" ,

CodePudding user response:

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.

  • Related