prefer_ inlined_ adds
Details about the 'prefer_inlined_adds' diagnostic produced by the Dart analyzer.
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.12.2 版本报告页面问题.