In Dart, there’s no concept of "uninitialized memory". Everything must be initialized before use, otherwise a compile-time error is reported. In
case of non-nullable type, it has to be explicitly initialized before use, and it can’t be initialized with null
. This is guaranteed by
compiler. In case of non-nullable variable, it will be set to null
implicitly. In both cases there is no need to initialize a variable
with null
.
Exceptions
In case of final
and const
variables or members, they have to be initialized explicitly, so using null
there
won’t trigger this rule.
const int? x = null;
Noncompliant code example
void f() {
int? x = null;
g(x);
}
Compliant solution
void f() {
int? x;
g(x);
}