unnecessary_ null_ in_ if_ null_ operators
Unnecessary use of '??' with 'null'.
Description
#
The analyzer produces this diagnostic when the right operand of the
??
operator is the literal
null
.
Example
#
The following code produces this diagnostic because the right-hand operand
of the
??
operator is
null
:
String? f(String? s) => s ?? null;
Common fixes
#If a non-null value should be used for the right-hand operand, then change the right-hand side:
String f(String? s) => s ?? '';
If there is no non-null value to use for the right-hand operand, then remove the operator and the right-hand operand:
String? f(String? s) => 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.