Getting wrong warning in Dart (possible bug), (The operand can't be null, so the condition is always 'true'.)

52 Views Asked by At

Here is my Dart program:

void main() {
  bool? b;
  if(b==true){
    print('b is true');
  }else if(b==false){
    print('b is false');
  }else if(b==null){
    print('b is null');
  }else{
    print('nothing!');
  }
}

it outputs this as expected:

b is null

but for line,

else if(b==null)

it's showing this warning:

The operand can't be null, so the condition is always 'true'.

warning image

but operand 'b' can be null in the program, as 'b' is nullable(bool?), so it shouldn't show the warning.

it's showing the warning in both IntelliJ IDEA and in https://dartpad.dev/

looks like a bug to me.

1

There are 1 best solutions below

0
Anup Barua On

if i add little more code like this, the warning is goes away:

import 'dart:io';
void main() {
  bool? b;  
  String? input  = stdin.readLineSync();
  if(input=='t'){
    b=true;
  }else if(input=='f'){
    b=false;
  }else{
    //no step
  }  
  if(b==true){
    print('b is true');
  }else if(b==false){
    print('b is false');
  }else if(b==null){
    print('b is null');
  }else{
    print('nothing!');
  }
}