use_if_null_to_convert_nulls_to_bools
Use an if-null operator to convert a 'null' to a 'bool'.
Description
#The analyzer produces this diagnostic when a nullable bool
-valued
expression is compared (using ==
or !=
) to a boolean literal.
Example
#The following code produces this diagnostic because the nullable boolean
variable b
is compared to true
:
dart
void f(bool? b) {
if (b == true) {
// Treats `null` as `false`.
}
}
Common fixes
#Rewrite the condition to use ??
instead:
dart
void f(bool? b) {
if (b ?? false) {
// Treats `null` as `false`.
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。