Skip to main content

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 ?? '';