ListUtils<T> extension

Extensions on List for real-world utility operations.

on

Properties

average double

Available on List<T>, provided by the ListUtils extension

Returns the average of a List<num>. Returns 0 if empty.
no setter
lastOrNull → T?

Available on List<T>, provided by the ListUtils extension

Returns the last element, or null if the list is empty.
no setter
median double

Available on List<T>, provided by the ListUtils extension

Returns the median of a List<num>. Returns 0 if empty.
no setter
mode → T?

Available on List<T>, provided by the ListUtils extension

Returns the most frequently occurring element, or null if empty.
no setter
penultimate → T?

Available on List<T>, provided by the ListUtils extension

Returns the second-to-last element, or null if the list has fewer than 2 items.
no setter
second → T?

Available on List<T>, provided by the ListUtils extension

Returns the second element, or null if the list has fewer than 2 items.
no setter
third → T?

Available on List<T>, provided by the ListUtils extension

Returns the third element, or null if the list has fewer than 3 items.
no setter

Methods

averageBy(num f(T)) double

Available on List<T>, provided by the ListUtils extension

Returns the average of the list by applying f to each element. Returns 0 if the list is empty.
chunk(int size) List<List<T>>

Available on List<T>, provided by the ListUtils extension

Splits the list into chunks of size.
clearAndAddAll(List<T> items) → void

Available on List<T>, provided by the ListUtils extension

Clears this list and adds all elements from items.
compact() List<T>

Available on List<T>, provided by the ListUtils extension

Removes null and empty (String/Iterable/Map) elements.
containsAllList(List<T> items) bool

Available on List<T>, provided by the ListUtils extension

Returns true if this list contains all elements of items.
cumulativeSum() List<num>

Available on List<T>, provided by the ListUtils extension

Returns the running cumulative sum of a List<num>.
difference(List<T> other) List<T>

Available on List<T>, provided by the ListUtils extension

Returns elements in this list that are NOT in other.
distinct() List<T>

Available on List<T>, provided by the ListUtils extension

Returns a new list with duplicate elements removed (preserves order).
distinctBy<K>(K key(T)) List<T>

Available on List<T>, provided by the ListUtils extension

Returns a new list with distinct elements based on key.
firstWhereOrNull(bool predicate(T)) → T?

Available on List<T>, provided by the ListUtils extension

Returns the first element satisfying predicate, or null if none found.
flatten<R>() List<R>

Available on List<T>, provided by the ListUtils extension

Flattens a List<List<R>> into a List<R>.
frequencies() Map<T, int>

Available on List<T>, provided by the ListUtils extension

Returns a map of each element to its occurrence count.
groupBy<K>(K key(T)) Map<K, List<T>>

Available on List<T>, provided by the ListUtils extension

Groups elements by key, returning a Map<K, List<T>>.
insertAt(int index, T item) → void

Available on List<T>, provided by the ListUtils extension

Inserts item at index, clamped to valid range.
intersection(List<T> other) List<T>

Available on List<T>, provided by the ListUtils extension

Returns elements that appear in both this list and other.
intersperse(T separator) List<T>

Available on List<T>, provided by the ListUtils extension

Returns a new list with separator inserted between every element.
joinToString(String separator, {String prefix = '', String suffix = '', String transform(T)?}) String

Available on List<T>, provided by the ListUtils extension

Joins elements to a string with separator, optional prefix/suffix, and optional transform.
mergeList(List<T> other, {bool unique = false}) List<T>

Available on List<T>, provided by the ListUtils extension

Merges this list with other. If unique is true, duplicates are excluded.
move(int from, int to) → void

Available on List<T>, provided by the ListUtils extension

Moves the element at from to to, shifting other elements.
page(int page, int pageSize) List<T>

Available on List<T>, provided by the ListUtils extension

Returns a page of elements (1-indexed).
partition(bool predicate(T)) Pair<List<T>, List<T>>

Available on List<T>, provided by the ListUtils extension

Splits the list into a Pair based on predicate. First list: elements where predicate is true. Second list: elements where predicate is false.
random({int? seed}) → T

Available on List<T>, provided by the ListUtils extension

Returns a random element from the list. Throws StateError if the list is empty.
removeAll(T item) → void

Available on List<T>, provided by the ListUtils extension

Removes all occurrences of item from the list.
removeFirst(T item) → void

Available on List<T>, provided by the ListUtils extension

Removes the first occurrence of item from the list.
removeLastOccurrence(T item) → void

Available on List<T>, provided by the ListUtils extension

Removes the last occurrence of item from the list.
removeN(T item, int n) → void

Available on List<T>, provided by the ListUtils extension

Removes the first n occurrences of item (or last if n is negative).
rotate(int n) List<T>

Available on List<T>, provided by the ListUtils extension

Rotates the list left by n positions (negative = right rotation).
sample(int n, {int? seed}) List<T>

Available on List<T>, provided by the ListUtils extension

Returns n random elements without replacement.
sortedBy<K extends Comparable>(K key(T)) List<T>

Available on List<T>, provided by the ListUtils extension

Returns a new list sorted ascending by key.
sortedByDescending<K extends Comparable>(K key(T)) List<T>

Available on List<T>, provided by the ListUtils extension

Returns a new list sorted descending by key.
sumBy(num f(T)) num

Available on List<T>, provided by the ListUtils extension

Sums the list by applying f to each element.
swap(int i, int j) → void

Available on List<T>, provided by the ListUtils extension

Swaps elements at indices i and j in place.
toMap<K, V>(K key(T), V value(T)) Map<K, V>

Available on List<T>, provided by the ListUtils extension

Converts this list to a Map<K, V> using key and value selectors.
toPairs() List<Pair<T, T>>

Available on List<T>, provided by the ListUtils extension

Returns consecutive pairs of elements.
transpose() List<List<T>>

Available on List<T>, provided by the ListUtils extension

Transposes a List<List<T>> (rows become columns).
windowed(int size, {int step = 1}) List<List<T>>

Available on List<T>, provided by the ListUtils extension

Returns a sliding window of size over the list.
zip<U>(List<U> other) List<Pair<T, U>>

Available on List<T>, provided by the ListUtils extension

Zips this list with other into a list of Pairs. Stops at the shorter list.