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
:
dart
String f(String? s) => s == null ? '' : s;
Common fixes
#Use the if-null operator instead:
dart
String f(String? s) => s ?? '';
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-08. View source or report an issue.