Skip to main content

prefer-last

has auto-fix
free+

Suggests using last instead of accessing a list by index (list[list.length - 1]) or calling iterable.elementAt(iterable.length - 1).

Example

❌ Bad:

const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];

// LINT: Prefer '.last' instead of accessing the last element by index.
list.elementAt(list.length - 1);
// LINT: Prefer '.last' instead of accessing the last element by index.
list[list.length - 1];

✅ Good:

const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];

list.last;