Dart Iterable Extensions
Table of contents
- where() method
- sublList() method
- contains() method
- skip() method
- skipWhile() method
- take() method
- takeWhile() method
- sort() method
- suffle() method
- singleWhere() method
- removeLast() method
- removeAt() method
- removeRange() method
- reduce() method
- replaceRange() method
- retainWhere() method
- lastWhere() method
- fold() method
- map() method
- last() method
- lastIndex() method
- element() method
- firstWhere() method
- getRange() method
- indexOf() method
- indexWhere() method
- insert() method
- insertAll() method
- join() method
where() method
This method use for find find value in the list for Int.
sublList() method
Dart provides the sublist()
method for lists, which allows you to extract a portion of a list based on the provided start and end indices.
contains() method
contains()
method is used to check whether a collection (such as a List, Set, or Map) contains a specific element. It returns true
if the element is found in the collection, and false
otherwise.
skip() method
This method skips the first count
elements in the iterable and returns an iterable that starts after skipping those elements.
skipWhile() method
This method skips elements from the beginning of the iterable as long as the condition specified by the test
function holds true.
Once the condition becomes false, it stops skipping and returns the remaining elements as a new iterable.
take() method
The take method retrieves a specified number of elements from the beginning of the iterable.
takeWhile() method
This method takes elements from the beginning of the iterable as long as the condition specified by the test
function holds true.
Once the condition becomes false, it stops taking elements and returns them as a new iterable.
sort() method
The sort()
method is part of the List
class, not directly extension of Iterable
.
However, since List
implements Iterable
, it can be used with lists as well as with iterables.
The sort()
method sorts the elements of a list in ascending order according to their natural ordering or based on a custom comparator function that you provide.
suffle() method
In Dart, the shuffle()
method is available for lists, not directly as an extension of the Iterable
class.
However, since List
implements Iterable
, you can use shuffle()
with lists as well as with iterables.
The shuffle()
method randomly shuffles the elements of a list. It does not return anything; instead, it shuffles the elements in place.
singleWhere() method
In Dart, the singleWhere()
method is used to find the single element in a collection that satisfies a given condition.
It returns the single element that matches the condition, or throws an error if there is not exactly one matching element.
removeLast() method
The removeLast()
method removes and returns the last element of the list. This operation modifies the list.
removeAt() method
The removeAt()
method removes the element at the specified index from the list and returns it.
The remaining elements are shifted to fill the removed element's position.
removeRange() method
The removeRange()
method removes a range of elements from the list, starting from the specified start index (inclusive) and ending at the specified end index (exclusive).
The elements within the specified range are removed, and the remaining elements are shifted to fill the gap.
reduce() method
Dart, the reduce()
method is available for iterables and is used to combine the elements of the iterable into a single value.
It applies a specified function cumulatively to the elements of the iterable from left to right to reduce them to a single value.
replaceRange() method
The replaceRange()
method is used to replace a range of elements in a list with another list of elements.
It allows you to specify the start index (inclusive) and the end index (exclusive) of the range to be replaced.
retainWhere() method
In Dart, the retainWhere()
method is used to retain only the elements of an iterable that satisfy a specified condition.
It removes all elements from the iterable that do not satisfy the condition.
lastWhere() method
In Dart, the lastWhere()
method is used to find the last element in an iterable that satisfies a specified condition.
It iterates over the elements of the iterable from the end and returns the last element that matches the condition.
fold() method
In Dart, the fold()
method is commonly used with iterables to accumulate values and reduce them to a single result.
The initial value for accumulation is 0
.
The combining function (previousValue, element) => previousValue + element
adds each element to the previousValue
.
map() method
In Dart, the map()
method is part of the Iterable
class, which represents a sequence of elements that can be accessed sequentially.
The map()
method applies a given function to each element of the iterable and returns a new iterable containing the results.
The function (int n) => n * n
squares each integer n
, and the resulting iterable squaredNumbers
contains the squared values.
last() method
last()
method is used to retrieve the last element from the ieterable.
lastIndex() method
lastIndex(
) is used to find the last occurrence of a specified element in the list.
In this example, the lastIndexOf()
method is used to find the last occurrence of the number 2
in the list numbers
.
It returns 5
, which is the index of the last occurrence of 2
in the list.
element() method
It allows you to retrieve the element at a specified index in the iterable.
firstWhere() method
It allows you to find the first element in the iterable that satisfies a specified condition.
In this example, the firstWhere()
method is used to find the first even number in the list numbers
. The condition number % 2 == 0
checks if the number is even.
If no even number is found, -1
is returned as specified by the orElse
parameter.
getRange() method
This method return the value using the index.
When we need data in particular range at that time this method is very useful.
indexOf() method
This method is use for the print index value of the data.
This will return only one index value at the time and print the first data with met condition.
indexWhere() method
This method use for find the index value of list string data using only one character.
in this method we also have to use statsWith or endsWith method to check by single character.
insert() method
This method use for insert single element in list on any index for this first we have to set index and then value which we want to insert in data.
insertAll() method
This method is use to insert multiple data at one time where insert add only one value at time where insertAll add multiple value at one time.
For this we have to store value in one variable then we can call this variable in our method with index the all the data of variable can add in the data.
join() method
This method use for join the data.
There are two types of method and syntax of join.
First, we can only declare single data type and join the value.
var nameList = <int>{1, 2, 3};
var join =nameList.join("-") ;
For this , Type Syntax is =
The Other type we can declare two type of data type in the variable such as <int , String> , <int ,double> , <String, double> ,< String ,int> , <double, int> , < double ,String>, when we declare like this then it work only for second data type not for both.
When we declare two data type then we must add values in our syntax our wise it's throw error in our code.
Subscribe to my newsletter
Read articles from Jinali Ghoghari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by