unnecessary_ null_ aware_ assignments
Unnecessary assignment of 'null'.
Description
#
The analyzer produces this diagnostic when the right-hand side of a
null-aware assignment is the
null
literal.
Example
#
The following code produces this diagnostic because the null aware
operator is being used to assign
null
to
s
when
s
is already
null
:
void f(String? s) {
s ??= null;
}
Common fixes
#If a non-null value should be assigned to the left-hand operand, then change the right-hand side:
void f(String? s) {
s ??= '';
}
If there is no non-null value to assign to the left-hand operand, then remove the assignment:
void f(String? s) {
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.