unchecked_ use_ of_ nullable_ value
Details about the 'unchecked_use_of_nullable_value' diagnostic produced by the Dart analyzer.
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-nullablePotentially non-nullableA type that is either non-nullable explicitly or due to
being a type parameter.
Learn more
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) {
// ...
}
}
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.