prefer-if-null-operators

Same as prefer_if_null_operators in Dart lints

Path
lint/complexity/prefer-if-null-operators
Default
On (recommended)
Origin
Dart lints
Rule
367 of 512
recommended

Flags x == null ? y : x (and the inverted x != null ? x : y).

A ternary that returns its own operand on the non-null branch is exactly the ?? if-null operator: x ?? y. The operator is shorter and evaluates x only once. The rule takes the operand of the == null/!= null check and compares it (ignoring whitespace) against the branch that falls through to it, firing when the two are textually equal.

Invalid

example.dartdart
void f(int? a, int? b, String fallback, C obj) {
  var r1 = a == null ? b : a;
  var r2 = a != null ? a : b;
  var r3 = null == a ? b : a;
  var r4 = a == null ? 0 : a;
  var r5 = obj.name == null ? fallback : obj.name;
  print('$r1 $r2 $r3 $r4 $r5');
}

class C {
  String? name;
}
Prefer using the if-null operator `??`.
1void f(int? a, int? b, String fallback, C obj) {
2 var r1 = a == null ? b : a;
Prefer using the if-null operator `??`.
2 var r1 = a == null ? b : a;
3 var r2 = a != null ? a : b;
Prefer using the if-null operator `??`.
3 var r2 = a != null ? a : b;
4 var r3 = null == a ? b : a;
Prefer using the if-null operator `??`.
4 var r3 = null == a ? b : a;
5 var r4 = a == null ? 0 : a;
Prefer using the if-null operator `??`.
5 var r4 = a == null ? 0 : a;
6 var r5 = obj.name == null ? fallback : obj.name;

Valid

example.dartdart
void f(int? a, int? b, C obj) {
  var r1 = a == null ? a : b;
  var r2 = a == null ? b : b;
  var r3 = a != null ? b : a;
  var r4 = a! > 0 ? a : b;
  var r5 = obj.name == null ? a : obj.other;
  var r6 = a ?? b;
  print('$r1 $r2 $r3 $r4 $r5 $r6');
}

class C {
  int? name;
  int? other;
}

Configure prefer-if-null-operators

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "prefer-if-null-operators": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/complexity/prefer-if-null-operators: <reason>
  • prefer-immediate-returnprefer-immediate-return

    Flags a local variable that is returned on the immediately following statement.

  • prefer-iterable-anyprefer_iterable_anyrecommended

    Flags iterable.where(...).isNotEmpty.

  • prefer-iterable-everyprefer_iterable_everyrecommended

    Flags iterable.where(predicate).isEmpty and filtered-length comparisons.

  • prefer-moving-to-variableprefer-moving-to-variable

    Flags repeated property/getter chains in local variable initializers.

  • prefer-null-aware-operatorsprefer_null_aware_operatorsrecommended

    Flags x == null ? null : x.y (and the inverted x != null ? x.y : null).

  • unnecessary-constunnecessary_constrecommended

    Flags a redundant const keyword inside an already-const context.

  • unnecessary-getters-settersunnecessary_getters_settersrecommended

    Flags a trivial getter/setter pair that only reads and writes a private field.

  • unnecessary-lambdasunnecessary_lambdasrecommended

    Flags a closure that only forwards its parameters to a function a tear-off could name.