prefer_inlined_adds
The addition of a list item could be inlined.
The addition of multiple list items could be inlined.
Description
#The analyzer produces this diagnostic when the methods add
and addAll
are invoked on a list literal where the elements being added could be included in the list literal.
Example
#The following code produces this diagnostic because the add
method is being used to add b
, when it could have been included directly in the list literal:
List<String> f(String a, String b) {
return [a]..add(b);
}
The following code produces this diagnostic because the addAll
method is being used to add the elements of b
, when it could have been included directly in the list literal:
List<String> f(String a, List<String> b) {
return [a]..addAll(b);
}
Common fixes
#If the add
method is being used, then make the argument an element of the list and remove the invocation:
List<String> f(String a, String b) {
return [a, b];
}
If the addAll
method is being used, then use the spread operator on the argument to add its elements to the list and remove the invocation:
List<String> f(String a, List<String> b) {
return [a, ...b];
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.