Skip to content

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
  • [value].first, .last or .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 if or a for;
  • ...?;
  • 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.

Violating
int first(int value) => [value].first;
Corrected
int first(int value) => value;

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.

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.