Dart Iterable Extensions
Table of contents
- where() method
- sublList() method
- contains() method
- sort() method
- shuffle() method
- singleWhere() method
- removeLast() method
- removeAt() method
- removeRange()
- replaceRange()
- lastWhere() method
- lastIndexOf() method
- elementAt()
- firstWhere()
- getRange() method
- indexOf() method
- indexWhere() method
- insert() method
- insertAll() method
- join() method
- skip Method:
- skipWhile Method:
- take Method:
- takeWhile Method:
- fold Method:
- map Method:
- last Method:
There are various Iterable Extensions in Dart , and I am providing info of most usable Iterable Extensions methods in dart.
where() method
- This method use for find find value in the list for Int.
final numbers = <int>[1, 2, 3, 5, 6, 7];
var result = numbers.where((x) => x < 5); // (1, 2, 3)
result = numbers.where((x) => x > 5); // (6, 7)
result = numbers.where((x) => x.isEven); // (2, 6)
sublList() method
- Tis method is use for to print string using the index , in this we can given the range or also we can only put only start index than complier give all the value after this index number.
final colors = <String>['red', 'green', 'blue', 'orange', 'pink'];
print(colors.sublist(1, 3)); // [green, blue]
//If [end] is omitted, it defaults to the [length] of this list.
final colors = <String>['red', 'green', 'blue', 'orange', 'pink'];
print(colors.sublist(3)); // [orange, pink]
contains() method
This method use to check the value is in the list or not , output only be shown in the bool value means only "True" or " False ".
If the List conation the value or full fill condition than give the true as output otherwise it shown false on output screen.
void main()
{
List <String> numList = ["Jay", 'Vinit' , 'Rahul'];
bool name= numList.contains("Vinit");
print(name); //True
}
sort() method
This method use in List , set for the Unsorted data to sorted as ascending order like small number to large number.
void main() { List <int> numList = [1,2,3,5,6,54,8,148,51,581,8]; numList.sort(); print(numList); } //[1, 2, 3, 5, 6, 8, 8, 51, 54, 148, 581]
shuffle() method
- This method use in List , set for the Shuffle data of the List. Every time we run this command it will give different type of output.
void main()
{
List <int> numList = [1,2,3,5,6,54,8,148,51,581,8];
numList.shuffle();
print(numList);
}
//[8, 2, 8, 581, 3, 148, 6, 54, 1, 5, 51]
singleWhere() method
- This method use for the find single value from the data , if the value is two or man than two a that time it will throw error.
void main()
{
List <int> numList = [2 , 2 ,6];
var list= numList.singleWhere((element) => element > 5);
print(list);
}
//6
removeLast() method
- This method is use for the remove the last value of the data set.
void main()
{
List <int> numList = [2 , 2 ,6];
numList.removeLast();
print(numList);
}
//[2, 2]
removeAt() method
- This method is use for the remove data using the index.
void main()
{
List <int> numList = [2 , 3 ,6];
numList.removeAt(0);
print(numList);
}
//[3, 6]
removeRange()
- Using this method we can remove the certain data of the data using the index , in this method we have to give two index , start and end , then compiler delete the value between this range.
void main()
{
List <int> numList = [2,6,9,8,5,5,7,3,6];
numList.removeRange(0 , 5);
print(numList);
}
//[5, 7, 3, 6]
replaceRange()
- This method use to enter data at the certain number of the index. for this we must create one variable and store the value in it and after we can get the use this method to enter the data in certain index.
void main()
{
List <int> numList = [2,6,9,8,5,5,7,3,6];
var num=[52 , 40];
numList.replaceRange(3 ,4, num);
print(numList);
}
//[2, 6, 9, 52, 40, 5, 5, 7, 3, 6]
lastWhere() method
This method is use for the finding the last value with full fill with the condition and then give output.
This will print only one value and if the condition is not full fill than give output as -1.
void main()
{
List <int> numList = [2,6,9,8,5,5,7,3,6];
var num2=numList.lastWhere((element) => element > 3);
print(num2);//6
}
lastIndexOf() method
void main()
{
List <int> numList = [2,6,9,8,5,5,7,3,6];
var num2 = numList.lastIndexOf(9);
print(num2); // 2
}
elementAt()
- This method use for print index of the value using elementAt(); which return the element index value.
void main()
{
List <int> numList = [2,6,9,8,5,5,7,3,6];
var num2 = numList.elementAt(9);
print(num2); //2
}
firstWhere()
- This method return the first value which fulfill the condition first. Like here we put the condition that if element is big than "3" then print "num2" because we have stored the "numList" value in "num2" , that's why we use num2 for the print value.
void main()
{
List <int> numList = [2,6,9,8,5,5,7,3,6];
var num2=numList.firstWhere((element) => element > 3);
print(num2);//6
}
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.
void main()
{
List <int> numList = [2,6,9,8,5,5,7,3,6];
var num2=numList.getRange(2 , 6) ;
print(num2);//(9, 8, 5, 5)
}
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.
void main()
{
List <int> numList = [2,6,9,8,5,5,7,3,6];
var num2=numList.indexOf(5) ;
print(num2);//4
}
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 .
void main()
{
List <String> nameList = ["Vinit", "Rahul" , "Jay" , "Ajay"];
var name=nameList.indexWhere((element) => element.startsWith ("V")) ;
var name2=nameList.indexWhere((element) => element.endsWith ("y")) ;
print(name); //0
print(name2); //2
}
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.
void main()
{
List <String> nameList = ["Vinit", "Rahul" , "Jay" , "Ajay"];
nameList.insert( 4 ,"Ram") ;
print(nameList); // [Vinit, Rahul, Jay, Ajay, Ram]
}
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.
void main() { List <String> nameList = ["Vinit", "Rahul" , "Jay" , "Ajay"]; var name2 = ["Jay","Ram" , "Hello"]; nameList.insertAll( 4 , name2) ; print(nameList); // [Vinit, Rahul, Jay, Ajay, Jay, Ram, Hello] }
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.
void main()
{
var nameList = <int, String>{1: "Ram " , 2: "Vinit ", 3 : "Jay"};
var join =nameList.values.join("-") ;
print(join); // Ram -Vinit -Jay
}
skip Method:
The skip method skips a specified number of elements from the beginning of the iterable.
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
Iterable<int> skippedNumbers = numbers.skip(2);
print(skippedNumbers); // Output: (3, 4, 5)
}
skipWhile Method:
The skipWhile method skips elements while a condition is true. Once the condition is false, it includes the remaining elements.
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
Iterable<int> skippedNumbers = numbers.skipWhile((number) => number < 3);
print(skippedNumbers); // Output: (3, 4, 5)
}
take Method:
The take method retrieves a specified number of elements from the beginning of the iterable.
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
Iterable<int> takenNumbers = numbers.take(3);
print(takenNumbers); // Output: (1, 2, 3)
}
takeWhile Method:
The takeWhile method retrieves elements while a condition is true. Once the condition is false, it stops taking elements.
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
Iterable<int> takenNumbers = numbers.takeWhile((number) => number < 4);
print(takenNumbers); // Output: (1, 2, 3)
}
These iterable extensions are useful for manipulating collections based on specific criteria, allowing you to skip or take elements as needed. The skip and take methods specify the number of elements directly, while skipWhile and takeWhile use a condition to determine when to skip or take elements.
fold Method:
The fold method is used to reduce the elements of an iterable to a single value. It takes an initial value and a combining function. The combining function is applied to each element and the current accumulated value.
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
int sum = numbers.fold(0, (int accumulator, int element) {
return accumulator + element;
});
print(sum); // Output: 15
}
In this example, the fold method is used to calculate the sum of all elements in the list.
map Method:
The map method transforms each element of an iterable using a provided function and returns a new iterable containing the transformed elements.
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
Iterable<String> stringNumbers = numbers.map((int number) {
return 'Number: $number';
});
print(stringNumbers); // Output: (Number: 1, Number: 2, Number: 3, Number: 4, Number: 5)
}
In this example, the map method is used to create a new iterable where each number is transformed into a string.
last Method:
The last method returns the last element in the iterable. It takes an optional predicate function to filter the elements.
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
int lastElement = numbers.last;
print(lastElement); // Output: 5
}