Iterable Extensions
This section provides documentation for extensions on the List and Iterable classes.
of
Get an element at a specific index in a null-safe manner.
final element = myList.of(2);final element = myList.of(2);tryRemoveAt
Attempts to remove an element at a given index in a null-safe manner.
myList.tryRemoveAt(1);myList.tryRemoveAt(1);indexOfOrNull
Find the index of an element within the list safely without throwing an error if the list is null.
final index = myList.indexOfOrNull(element);final index = myList.indexOfOrNull(element);indexWhereOrNull
Find the index of the first element matching the given predicate, returning null if the list is empty.
final index = myList.indexWhereOrNull((item) => item.id == id);final index = myList.indexWhereOrNull((item) => item.id == id);tryRemoveWhere
Attempts to remove all elements that satisfy the provided test.
myList.tryRemoveWhere((item) => item.id == id);myList.tryRemoveWhere((item) => item.id == id);Common Iterable Extensions
isEmptyOrNull and isNotEmptyOrNull
Properties to check if the iterable is null or empty, and its negation, respectively.
bool isEmpty = collection.isEmptyOrNull;
bool isNotEmpty = collection.isNotEmptyOrNull;bool isEmpty = collection.isEmptyOrNull;
bool isNotEmpty = collection.isNotEmptyOrNull;firstOrNull and lastOrNull
Get the first or last element of the iterable, or null if the iterable is empty.
final firstItem = collection.firstOrNull;
final lastItem = collection.lastOrNull;final firstItem = collection.firstOrNull;
final lastItem = collection.lastOrNull;firstOrNullWhere
Find the first element satisfying the provided predicate or returns null if no element matches.
final item = collection.firstOrNullWhere((item) => item.id == id);final item = collection.firstOrNullWhere((item) => item.id == id);lastOrDefault and firstOrDefault
Get the last or first element of the iterable, or the default value if the iterable is empty.
final lastName = names.lastOrDefault("Doe");
final firstName = names.firstOrDefault("John");final lastName = names.lastOrDefault("Doe");
final firstName = names.firstOrDefault("John");concatWithSingleList and concatWithMultipleList
Create a list by concatenating the iterable with another single or multiple iterables.
final allItems = items.concatWithSingleList(otherItems);
final allItems = items.concatWithMultipleList([list1, list2]);final allItems = items.concatWithSingleList(otherItems);
final allItems = items.concatWithMultipleList([list1, list2]);toMutableSet
Converts the iterable into a mutable set.
final itemSet = items.toMutableSet();final itemSet = items.toMutableSet();groupBy
Groups the elements of the iterable by the keys returned by the provided function.
final groupedItems = items.groupBy((item) => item.category);final groupedItems = items.groupBy((item) => item.category);filter and filterNot
Returns a list of elements that satisfy (or don't satisfy) a provided predicate.
final filteredItems = items.filter((item) => item.price > 20);
final excludedItems = items.filterNot((item) => item.isDiscounted);final filteredItems = items.filter((item) => item.price > 20);
final excludedItems = items.filterNot((item) => item.isDiscounted);halfLength, takeOnly, and drop
Utilities for manipulating the size and contents of the iterable.
final halfSize = items.halfLength;
final firstItems = items.takeOnly(5);
final remainingItems = items.drop(5);final halfSize = items.halfLength;
final firstItems = items.takeOnly(5);
final remainingItems = items.drop(5);sortedDescending, containsAll, and all
Sorting, containment, and predicate-checking utilities.
final sortedItems = items.sortedDescending();
final hasAll = items.containsAll(otherItems);
final allExpensive = items.all((item) => item.price > 100);final sortedItems = items.sortedDescending();
final hasAll = items.containsAll(otherItems);
final allExpensive = items.all((item) => item.price > 100);distinctBy
Returns a list of elements with distinct keys derived from a provided predicate.
final distinctAges = people.distinctBy((person) => person.age);final distinctAges = people.distinctBy((person) => person.age);