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
:
void f(bool? b) {
if (b == true) {
// Treats `null` as `false`.
}
}
Common fixes
#Rewrite the condition to use ??
instead:
void f(bool? b) {
if (b ?? false) {
// Treats `null` as `false`.
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.