unchecked_ use_ of_ nullable_ value
A nullable expression can't be used as a condition.
A nullable expression can't be used as an iterator in a for-in loop.
A nullable expression can't be used in a spread.
A nullable expression can't be used in a yield-each statement.
The function can't be unconditionally invoked because it can be 'null'.
The method '{0}' can't be unconditionally invoked because the receiver can be 'null'.
The operator '{0}' can't be unconditionally invoked because the receiver can be 'null'.
The property '{0}' can't be unconditionally accessed because the receiver can be 'null'.
Description
#
The analyzer produces this diagnostic when an expression whose type is
potentially non-nullable
is dereferenced without first verifying that
the value isn't
null
.
Example
#
The following code produces this diagnostic because
s
can be
null
at
the point where it's referenced:
void f(String? s) {
if (s.length > 3) {
// ...
}
}
Common fixes
#
If the value really can be
null
, then add a test to ensure that members
are only accessed when the value isn't
null
:
void f(String? s) {
if (s != null && s.length > 3) {
// ...
}
}
If the expression is a variable and the value should never be
null
, then
change the type of the variable to be non-nullable:
void f(String s) {
if (s.length > 3) {
// ...
}
}
If you believe that the value of the expression should never be
null
, but
you can't change the type of the variable, and you're willing to risk
having an exception thrown at runtime if you're wrong, then you can assert
that the value isn't null:
void f(String? s) {
if (s!.length > 3) {
// ...
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-4. View source or report an issue.