one_element_collection_read_back
Reports a one-element list or set literal built only to give its element back. Use the element itself.
- Tier
- advisory
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- writes the element in place of the reading or the spread, in brackets where needed
What it reports
Section titled “What it reports”[value].first,.lastor.single;- a spread of such a literal into another:
[...[value], ...others].
The violation covers the reading or the spread.
It does not report:
- a literal with type arguments,
<num>[count].first, which gives the element a type of its own; - a literal whose element is a spread, an
ifor afor; ...?;- a map.
The fix writes the element in place of the reading or the spread, in brackets where needed. It is not offered where a comment stands in the literal outside the element.
The literal is allocated, read and dropped. The element alone says the same.
Example
Section titled “Example”Violating
int first(int value) => [value].first;Corrected
int first(int value) => value;Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// Why it stays as written here.// ignore: one_element_collection_read_back// ignore_for_file: assay/one_element_collection_read_back silences it in a whole file, and the prefixed // ignore: assay/one_element_collection_read_back works too. See Suppressions.