ListUtils<T> extension

Extension methods for Lists.

on

Methods

chunk(int n) List<List<T>>
Split the list into chunks of size n. Returns a list of chunks.
clearAndAddAll(List<T> items) → void
Clear and add all elements of List items to the list.
compact() List<T>
Remove null or empty elements from the list. Returns a new list with null or empty elements removed.
containsAllList(List<T> items) bool
Check if the list contains all elements of List items. Returns true if the list contains all elements of items, false otherwise.
distinctBy(dynamic f(T)) List<T>
Distinct the list by f. Returns a new list with distinct elements based on f.
getFirst(T item) → T?
Get the first occurrence of item from the list. Returns null if the item was not found.
getLast(T item) → T?
Get the last occurrence of item from the list. Returns null if the item was not found.
groupBy<K>(K f(T)) Map<K, List<T>>
Group the list by f. Returns a Map with the keys and values returned by f.
joinToString(String separator, {String prefix = '', String suffix = '', String transform(T)?}) String
Join to String with separator, prefix and suffix, and transform function. Returns a String with the elements joined by separator, prefix and suffix, and transformed by transform. If transform is null, the elements are converted to String with toString().
mergeList(List<T> items, {bool unique = false}) List<T>
Merge the list with List items. Returns a new list with all elements of the list and items.
partition(bool f(T)) Pair<List<T>, List<T>>
Split the list into two lists based on f. Returns a list of two lists of T. The first list contains all elements for which f returns true. The second list contains all elements for which f returns false.
removeAll(T item) → void
Remove all occurrences of item from the list.
removeAllIterable(Iterable<T> items) → void
Remove all occurrences of Iterable items from the list.
removeAllList(List<T> items) → void
Remove all occurrences of List items from the list.
removeAllMapKeys(Map<T, dynamic> items) → void
Remove all occurrences of Map items keys from the list.
removeAllMapValues(Map<T, dynamic> items) → void
Remove all occurrences of Map items values from the list.
removeAllSet(Set<T> items) → void
Remove all occurrences of Set items from the list.
removeFirst(T item) → void
Remove the first occurrence of item from the list.
removeLast(T item) → void
Remove the last occurrence of item from the list.
removeN(T item, int n) → void
Remove first n occurrences of item from the list. If n is negative, remove from end of list.
removeNList(List<T> items, int n) → void
Remove first n occurrences of List items from the list. If n is negative, remove from end of list.
sortedBy(dynamic f(T)) List<T>
Sort the list by f. Returns a new list sorted by f.
sortedByDescending(dynamic f(T)) List<T>
Returns a new list sorted by f descending.
sumBy(num f(T)) num
Sum by f of all elements in the list. Returns 0 if the list is empty and f returns null.
takeIf(bool f(T)) List<T>
Take if f is true. Returns a new list with the elements taken if f is true.
takeWhile(bool f(T)) List<T>
Take while f is true. Returns a new list with the elements taken while f is true.
toMap<K, V>(K key(T), V value(T)) Map<K, V>
Convert the list to a Map with key and value functions. Returns a new Map with the keys and values returned by key and value.