avoid_init_to_null
Redundant initialization to 'null'.
Description
#The analyzer produces this diagnostic when a nullable variable is explicitly initialized to null
. The variable can be a local variable, field, or top-level variable.
A variable or field that isn't explicitly initialized automatically gets initialized to null
. There's no concept of "uninitialized memory" in Dart.
Example
#The following code produces this diagnostic because the variable f
is explicitly initialized to null
:
dart
class C {
int? f = null;
void m() {
if (f != null) {
print(f);
}
}
}
Common fixes
#Remove the unnecessary initialization:
dart
class C {
int? f;
void m() {
if (f != null) {
print(f);
}
}
}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-08. View source or report an issue.