ListExtensions<T> extension
Common list extensions
- on
-
- List<
T>
- List<
Properties
- isEmptyOrNull → bool
-
Checks if the list is empty or only contains null values.
no setter
- random → T?
-
Randomly gets an element from the list.
Returns null if the list is empty.
no setter
-
reversedList
→ List<
T> -
Returns a new list with the elements in reverse order.
no setter
-
shuffled
→ List<
T> -
Shuffles the elements of the list into a random order.
no setter
-
unique
→ List<
T> -
Returns a new list containing only the unique elements from the original list.
no setter
Methods
-
addIfNotContains(
T element) → void - Adds an element to the list if it does not already contain it.
-
chunked(
int chunkSize) → List< List< T> > - Creates subLists of a given size from the list.
-
firstWhereOrElse(
bool test(T), T orElse) → T -
Finds the first element matching
test
, or returnsorElse
if none match. -
firstWhereOrNull(
bool test(T)) → T? -
Tries to find the first element in the list that matches the given
test
. Returns null if no such element is found. -
groupBy<
K> (K keyFunction(T)) → Map< K, List< T> > -
Groups the elements in the list according to the
keyFunction
. Returns a map where each key corresponds to a list of elements that share that key. -
mapToList<
E> (E convert(T)) → List< E> -
Converts each element in the list to another type using the
convert
function. Returns a new list containing the converted elements. -
removeFirstWhere(
bool test(T)) → bool -
Removes the first element in the list that satisfies the given
test
. Returns true if an element was removed, otherwise false. -
safeElementAt(
[int? index]) → T? -
Safely gets an element from the list at the given
index
. Returns null if theindex
is out of bounds.