takeLast method

List<E> takeLast(
  1. int n
)

Returns a list containing last n elements.

val chars = [1, 2, 3, 4, 5, 6, 7, 8, 9];
print(chars.take(3)) // [1, 2, 3]
print(chars.takeWhile((it) => it < 5) // [1, 2, 3, 4]
print(chars.takeLast(2)) // [8, 9]
print(chars.takeLastWhile((it) => it > 5 }) // [6, 7, 8, 9]

Implementation

List<E> takeLast(int n) {
  var list = this is List<E> ? this as List<E> : toList();
  return list.sublist(length - n);
}