Arr class

Static utility methods for working with lists.

Arr.wrap('foo');                  // ['foo']
Arr.flatten([1, [2, [3]]]);       // [1, 2, 3]
Arr.chunk([1, 2, 3, 4, 5], 2);    // [[1, 2], [3, 4], [5]]
Arr.pluck(users, 'name');         // ['Anna', 'Brad']
Arr.keyBy(users, 'id');           // {1: {...}, 2: {...}}

Available helpers:

Type checks:

Construction:

Filtering:

Slicing / chunking:

Ordering:

Random:

Iteration:

Mutations (immutable):

Aggregates:

Map-list operations:

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

accessible(Object? value) bool
Returns true when value is a List or Map (i.e. index/key accessible).
average<T>(Iterable<T> list, {num by(T)?}) double
Returns the arithmetic mean of list, or 0 when empty.
chunk<T>(Iterable<T> list, int size) List<List<T>>
Splits list into chunks of size elements.
collapse<T>(Iterable<Iterable<T>> arrays) List<T>
Collapses a list of lists into a single list.
countBy<T, K>(Iterable<T> list, {K by(T)?}) Map<K, int>
Counts occurrences in list, optionally grouping by the value of by.
crossJoin(Iterable<Iterable> lists) List<List>
Cross joins the given lists, returning every possible combination.
every<T>(Iterable<T> list, bool predicate(T)) bool
Returns true when every element of list passes predicate. Vacuously true for empty lists.
exceptValues<T>(Iterable<T> list, Iterable<T> values) List<T>
Returns list with each of values removed.
first<T>(Iterable<T> list, {bool predicate(T)?, T? defaultValue}) → T?
Returns the first element matching predicate, or defaultValue.
flatMap<T, R>(Iterable<T> list, Iterable<R> fn(T)) List<R>
Maps each element of list through fn, flattening the resulting iterables into a single list.
flatten(Iterable list, {int depth = -1}) List
Flattens a nested iterable into a single-level list.
groupBy<T, K>(Iterable<T> list, K by(T)) Map<K, List<T>>
Groups list into a map keyed by the value returned by by.
indexed<T>(Iterable<T> list) List<(int, T)>
Pairs each element of list with its index, returning a list of (index, value) records.
interleave<T>(Iterable<T> list, T separator) List<T>
Returns list with separator inserted between every pair of elements.
isAssoc(Object? value) bool
Returns true when value is a Map (associative).
isList(Object? value) bool
Returns true when value is a List.
join(Iterable list, [String separator = ', ', String finalSeparator = '']) String
Joins list into a string with separator; the final element is joined with finalSeparator when provided.
keyBy<K, V>(Iterable<Map<K, V>> list, K key) Map<V, Map<K, V>>
Indexes list by the value at key. Entries with a null value at key are skipped.
last<T>(Iterable<T> list, {bool predicate(T)?, T? defaultValue}) → T?
Returns the last element matching predicate, or defaultValue.
map<T, R>(Iterable<T> list, R callback(T value, int index)) List<R>
Maps each element of list through callback, passing the index too.
mapWithKeys<T, K, V>(Iterable<T> list, MapEntry<K, V> callback(T)) Map<K, V>
Maps each element of list to a MapEntry, returning the resulting map.
max<T>(Iterable<T> list, {Comparable by(T)?}) → T
Returns the element of list with the largest value of by (or the largest element itself when by is null and T is Comparable).
median<T>(Iterable<T> list, {num by(T)?}) double
Returns the median of list, or 0 when empty.
min<T>(Iterable<T> list, {Comparable by(T)?}) → T
Returns the element of list with the smallest value of by (or the smallest element itself when by is null and T is Comparable).
move<T>(Iterable<T> list, int from, int to) List<T>
Returns a new list with the element at from moved to position to.
onlyValues<T>(Iterable<T> list, Iterable<T> values) List<T>
Returns the elements of list that are also in values.
partition<T>(Iterable<T> list, bool predicate(T)) List<List<T>>
Splits list into a [matching, nonMatching] pair based on predicate.
pluck<K, V>(Iterable<Map<K, V>> list, K key) List<V?>
Plucks the value at key from each map in list.
prepend<T>(Iterable<T> list, T value) List<T>
Returns a new list with value inserted at the front.
push<T>(Iterable<T> list, T value) List<T>
Returns a new list with value appended at the end.
random<T>(List<T> list) → T
Returns a random element of list. Throws when list is empty.
randomMany<T>(List<T> list, int count) List<T>
Returns up to count random elements of list without replacement.
reject<T>(Iterable<T> list, bool predicate(T)) List<T>
Filters list to elements NOT matching predicate. Inverse of where.
replaceAt<T>(Iterable<T> list, int index, T value) List<T>
Returns a new list with the element at index replaced by value. Throws RangeError when index is out of range.
select<K, V>(Iterable<Map<K, V>> list, Iterable<K> keys) List<Map<K, V>>
Returns each map in list reduced to only the given keys.
shuffle<T>(Iterable<T> list, {int? seed}) List<T>
Returns a shuffled copy of list. Pass seed for determinism.
sole<T>(Iterable<T> list, {bool predicate(T)?}) → T
Returns the single element matching predicate. Throws if zero or more than one element matches.
some<T>(Iterable<T> list, bool predicate(T)) bool
Returns true when at least one element of list passes predicate.
sort<T>(Iterable<T> list, {int compare(T, T)?}) List<T>
Returns a sorted copy of list.
sortDesc<T extends Comparable>(Iterable<T> list) List<T>
Returns a sorted copy of list in descending order.
sortRecursive(List list, {int compare(dynamic, dynamic)?}) List
Recursively sorts list. Nested lists are sorted at every depth; values of mixed types fall back to string comparison.
sum<T>(Iterable<T> list, {num by(T)?}) num
Returns the sum of list. Pass by to extract a number from each element; otherwise list must be Iterable<num>.
swap<T>(Iterable<T> list, int i, int j) List<T>
Returns a new list with the elements at i and j swapped.
take<T>(Iterable<T> list, int count) List<T>
Returns the first count elements of list, or last count if negative.
unique<T>(Iterable<T> list) List<T>
Returns the unique values of list.
where<T>(Iterable<T> list, bool predicate(T)) List<T>
Filters list to elements matching predicate.
whereNotNull<T>(Iterable<T?> list) List<T>
Returns list with null values removed.
wrap(Object? value) List
Wraps value in a List if it isn't already one. Returns an empty list when value is null.