prefer-null-aware-method-calls

Same as prefer_null_aware_method_calls in Dart lints

Group
Style
Path
lint/style/prefer-null-aware-method-calls
Default
On (recommended)
Origin
Dart lints
Rule
391 of 512
recommended

Prefers f?.call() to calling a null-checked function after a != null test.

if (f != null) f!(); and f != null ? f!() : null both invoke a nullable function only when it is non-null; f?.call() says the same thing directly. The rule fires when the condition is <expr> != null (null on the right), the if has no else and its then-branch is that single call (optionally in a block), or the conditional's else branch is null, and the call's function is <expr>! with <expr> textually equal to the tested expression. Parenthesized conditions, nulls, calls and callees do not match; compared operands retain their parentheses, as in the official rule.

Invalid

example.dartdart
class Holder {
  void Function()? callback;
  int Function(int)? transform;
}

void ifStatement(void Function()? f) {
  if (f != null) f!();
}

void ifBlock(void Function(int)? f) {
  if (f != null) {
    f!(1);
  }
}

void field(Holder holder) {
  if (holder.callback != null) holder.callback!();
}

int? conditional(int Function(int)? f) => f != null ? f!(2) : null;

int? conditionalField(Holder h) =>
    h.transform != null ? h.transform!(3) : null;

void spacing(void Function()? f) {
  if (f!= null) f !();
}
Use a null-aware invocation of the `call` method rather than explicitly testing for null.
6void ifStatement(void Function()? f) {
7 if (f != null) f!();
Use a null-aware invocation of the `call` method rather than explicitly testing for null.
11 if (f != null) {
12 f!(1);
Use a null-aware invocation of the `call` method rather than explicitly testing for null.
16void field(Holder holder) {
17 if (holder.callback != null) holder.callback!();
Use a null-aware invocation of the `call` method rather than explicitly testing for null.
19
20int? conditional(int Function(int)? f) => f != null ? f!(2) : null;
Use a null-aware invocation of the `call` method rather than explicitly testing for null.
22int? conditionalField(Holder h) =>
23 h.transform != null ? h.transform!(3) : null;
Use a null-aware invocation of the `call` method rather than explicitly testing for null.
25void spacing(void Function()? f) {
26 if (f!= null) f !();

Valid

example.dartdart
class Holder {
  void Function()? callback;
}

void nullAware(void Function()? f) {
  f?.call();
}

void withElse(void Function()? f) {
  if (f != null) {
    f!();
  } else {
    print('none');
  }
}

void twoStatements(void Function()? f) {
  if (f != null) {
    f!();
    print('done');
  }
}

void differentTarget(void Function()? f, void Function()? g) {
  if (f != null) g!();
}

void reversed(void Function()? f) {
  if (null != f) f!();
}

int? notNullElse(int Function()? f) => f != null ? f!() : 0;

void method(Holder h) {
  if (h.callback != null) h.callback?.call();
}

Configure prefer-null-aware-method-calls

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-null-aware-method-calls": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/prefer-null-aware-method-calls: <reason>