在 Dart 中收到错误警告(可能是错误),(操作数不能为空,因此条件始终为“true”。)

问题描述 投票:0回答:1

这是我的 Dart 程序:

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!');
  }
}

它按预期输出:

b is null

但是对于线路来说,

else if(b==null)

它显示此警告:

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

image of the warning

但是操作数'b'在程序中可以为空,因为'b'可以为空(bool?),所以它不应该显示警告。

它在 IntelliJ IDEA 和 https://dartpad.dev/

中显示警告

对我来说看起来像是一个错误。

dart intellij-idea warnings
1个回答
0
投票

如果我添加更多这样的代码,警告就会消失

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!');
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.