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
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
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});
}int? topLevel = null;int? topLevel = null;var inferred = null;class C { String? field = null; String? field = null; dynamic dyn = null; final closureField = () { var inner = null; void method() { int? local = null; int? local = null; var x = null; var x = null; double? a = null, b; void namedDefault({int? value = null}) {}void namedDefault({int? value = null}) {}void positionalDefault([int? value = null]) {} final int? value; FieldDefault({this.value = null});class RedundantSuperDefault extends NullSuperDefault { RedundantSuperDefault({super.value = null}); void parenthesizedDefault({int? value = (null)}) {}class OptionalPositionalChild extends RequiredPositionalBase { OptionalPositionalChild([super.value = null]);class OptionalNamedChild extends RequiredNamedBase { OptionalNamedChild({super.value = null});Valid
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.
{
"linter": {
"rules": {
"style": {
"avoid-init-to-null": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/avoid-init-to-null: <reason>More style rules
- avoid-js-rounded-ints
avoid_js_rounded_intsrecommendedAvoid integer literals that JavaScript would round.
- avoid-late-keyword
avoid-late-keywordFlags use of the
latekeyword on variables and fields. - avoid-multiple-declarations-per-line
avoid_multiple_declarations_per_linerecommendedFlags a variable declaration that declares more than one variable.
- avoid-non-null-assertion
avoid-non-null-assertionFlags the null-assertion operator
!except on a resolvedMapindex. - avoid-null-checks-in-equality-operators
avoid_null_checks_in_equality_operatorsrecommendedFlags null checks of the parameter inside a custom
operator ==. - avoid-positional-boolean-parameters
avoid_positional_boolean_parametersFlags a positional
boolparameter in a public signature. - avoid-positional-fields-in-records
avoid_positional_fields_in_recordsFlags positional fields in record types.
- avoid-private-typedef-functions
avoid_private_typedef_functionsFlags a private function typedef that is used at most once.