prefer_ if_ null_ operators
Use the '??' operator rather than '?:' when testing for 'null'.
Description
#
The analyzer produces this diagnostic when a conditional expression (using
the
?:
operator) is used to select a different value when a local
variable is
null
.
Example
#
The following code produces this diagnostic because the variable
s
is
being compared to
null
so that a different value can be returned when
s
is
null
:
String f(String? s) => s == null ? '' : s;
Common fixes
#Use the if-null operator instead:
String f(String? s) => s ?? '';
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.