void main() {
print("Conditional Operators:");
// small if else
var a1 = 100;
var b2 = 20;
var istrue = (a1 < b2) ? 'True' : 'False';
print(istrue);
// check if null or print name
var name1;
var check = name1 ?? "This is Null";
print(check);
var name = "Abdulelah";
var checknot = name ?? "This is Null";
print(name);
}
I don't how i fix this problem in line 16 yellow error said:
The left operand can't be null, so the right operand is never executed. Try removing the operator and the right operand.dartdead_null_aware_expression
CodePudding user response:
The variable "name" won't be NULL because you give to him the value "Abdulelah", so the right part ?? "This is NULL"
won't be executed, remove this right part and the warning will disappear.
CodePudding user response:
Using ??
we provide default value in null case.
a = x ?? y
The above example shows that if x
return/becomes null, y
will be initialized on a
. The fact becomes.
- if
x
is notnull
, thena = x
- if
x
isnull
, thena = y
Instead of using if, else
we use ??
to provide null handler value.
The editor is acting smart here because it knows that name
is not null, so the yellow message is saying you don't need to provide default value because left expression will never be null.
To create null
variable, you need to use
DataType? variableName;
In this case you are initializing String
and you can do
String? name = "Abdulelah";
But this is not needed unless you want to initialize null value on name
variable.
You can check
- Difference between
var
anddynamic
type in Dart? and dart.dev for more.