avoid-init-to-null

Same as avoid_init_to_null in Dart lints

Group
Style
Path
lint/style/avoid-init-to-null
Default
On (recommended)
Origin
Dart lints
Rule
76 of 512
recommended

Flags explicit = null initializers on declarations that already default to null.

A non-final, non-const, non-late variable whose type is nullable, absent, or dynamic is already initialized to null, so writing = null adds noise without changing behavior. Drop the initializer and let the default stand. Declarations that are final, const, late, or have a non-nullable type are left alone, since for them the null is either required or meaningful.

Invalid

example.dartdart
int? topLevel = null;
var inferred = null;

class C {
  String? field = null;
  dynamic dyn = null;

  // Regression: a `= null` nested inside a closure in a field initializer.
  final closureField = () {
    var inner = null;
    return inner;
  };

  void method() {
    int? local = null;
    var x = null;
    double? a = null, b;
    print('$local $x $a $b');
  }
}

void namedDefault({int? value = null}) {}
void positionalDefault([int? value = null]) {}

class FieldDefault {
  final int? value;
  FieldDefault({this.value = null});
}

class NullSuperDefault {
  NullSuperDefault({int? value});
}

class RedundantSuperDefault extends NullSuperDefault {
  RedundantSuperDefault({super.value = null});
}

void parenthesizedDefault({int? value = (null)}) {}

class RequiredPositionalBase {
  RequiredPositionalBase(int? value);
}

class OptionalPositionalChild extends RequiredPositionalBase {
  OptionalPositionalChild([super.value = null]);
}

class RequiredNamedBase {
  RequiredNamedBase({required int? value});
}

class OptionalNamedChild extends RequiredNamedBase {
  OptionalNamedChild({super.value = null});
}
Don't explicitly initialize variables to `null`.
1int? topLevel = null;
Don't explicitly initialize variables to `null`.
1int? topLevel = null;
2var inferred = null;
Don't explicitly initialize variables to `null`.
4class C {
5 String? field = null;
Don't explicitly initialize variables to `null`.
5 String? field = null;
6 dynamic dyn = null;
Don't explicitly initialize variables to `null`.
9 final closureField = () {
10 var inner = null;
Don't explicitly initialize variables to `null`.
14 void method() {
15 int? local = null;
Don't explicitly initialize variables to `null`.
15 int? local = null;
16 var x = null;
Don't explicitly initialize variables to `null`.
16 var x = null;
17 double? a = null, b;
Don't explicitly initialize variables to `null`.
21
22void namedDefault({int? value = null}) {}
Don't explicitly initialize variables to `null`.
22void namedDefault({int? value = null}) {}
23void positionalDefault([int? value = null]) {}
Don't explicitly initialize variables to `null`.
26 final int? value;
27 FieldDefault({this.value = null});
Don't explicitly initialize variables to `null`.
34class RedundantSuperDefault extends NullSuperDefault {
35 RedundantSuperDefault({super.value = null});
Don't explicitly initialize variables to `null`.
37
38void parenthesizedDefault({int? value = (null)}) {}
Don't explicitly initialize variables to `null`.
44class OptionalPositionalChild extends RequiredPositionalBase {
45 OptionalPositionalChild([super.value = null]);
Don't explicitly initialize variables to `null`.
52class OptionalNamedChild extends RequiredNamedBase {
53 OptionalNamedChild({super.value = null});

Valid

example.dartdart
int nonNull = 0;
late int? lazy;

class C {
  int? uninitialized;
  final String? name = null;
  const C();
  int count = 0;

  void method(int? param) {
    int? local;
    var x = 42;
    String value = 'hi';
    print('$local $x $value $param');
  }

  void withDefault({int? param = 1}) {
    print(param);
  }
}

class NonNullSuperDefault {
  NonNullSuperDefault({int? value = 3});
}

class ChangedSuperDefault extends NonNullSuperDefault {
  ChangedSuperDefault({super.value = null});
}

Configure avoid-init-to-null

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-init-to-null": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/avoid-init-to-null: <reason>