IterableExtensions<E> extension

A collection of helpful extensions on Iterable to reduce boilerplate.

on

Methods

chunk(int size) List<List<E>>

Available on Iterable<E>, provided by the IterableExtensions extension

Splits the iterable into chunks of the given size. Example: [1, 2, 3, 4, 5].chunk(2) -> [[1, 2], [3, 4], [5]]
groupBy<K>(K keyFunction(E)) Map<K, List<E>>

Available on Iterable<E>, provided by the IterableExtensions extension

Groups the elements in the iterable by a key returned by keyFunction. Example: users.groupBy((u) => u.role)
random() → E?

Available on Iterable<E>, provided by the IterableExtensions extension

Returns a random element from the iterable. Returns null if the iterable is empty. Example: [1, 2, 3].random()
sorted([int compare(E a, E b)?]) List<E>

Available on Iterable<E>, provided by the IterableExtensions extension

Returns a new sorted list from the iterable. Example: [3, 1, 2].sorted() -> [1, 2, 3]
unique([dynamic id(E)?]) List<E>

Available on Iterable<E>, provided by the IterableExtensions extension

Returns a list containing only unique elements. If id is provided, uniqueness is determined by the returned identifier. Example: [1, 1, 2].unique() -> [1, 2]