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];
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。