takeLast method
Last n elements; all if n >= length.
Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
List<T> takeLast(int n) {
// A non-positive count takes nothing; without this guard `sublist(length-n)`
// for negative n reads past the end and throws a RangeError.
if (n <= 0) return <T>[];
final List<T> list = toList();
if (n >= list.length) return list;
return list.sublist(list.length - n);
}