这是我的 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'.
但是操作数'b'在程序中可以为空,因为'b'可以为空(bool?),所以它不应该显示警告。
它在 IntelliJ IDEA 和 https://dartpad.dev/
中显示警告对我来说看起来像是一个错误。
如果我添加更多这样的代码,警告就会消失
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!');
}
}