Top 10 Array utility methods that you should know

In this guide, we will learn top 10 the array utility methods that are used in the dart. We will demonstrate it with the help of the examples.

1. map():

Produces a new list after transforming each element in a given list

void main() {
  var name = ['Flutter', 'Dart', 'Deno', 'Firebase'];

  var result = name.map((e) => 'I love $e');
  print(result);
}

The Output will be (I love Flutter, I love Dart, I love Deno, I love Firebase)

2. contains()

Checks to confirm that the given element is in the list

void main() {
  var name = ['Flutter', 'Dart', 'Deno', 'Firebase'];

  var result = name.contains('Deno');
  print(result);
}

The Output will be true

3. sort()

Orders the elements in a list based on the provided ordering function

void main() {
  List<String> name = [
    'sagar',
    'koju',
    'sanam',
    'keshav',
    'pancha',
    'mobile',
    'flutter',
    'deno',
    'dart',
  ];
 name.sort();
  print(name);
}

The Output will be [dart, deno, flutter, keshav, koju, mobile, pancha, sagar, sanam]

4. forEach()

Runs a function on each element in the list

void main() {
  List<String> name = ['sagar', 'koju', 'is', 'flutter', 'developer'];
  name.forEach((e) => print(e));
}

The Output will be sagar koju is flutter developer

5. reduce(), fold()

Compresses the elements of a list to a single value, using a given function

void main() {
 List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  List<int> numbers1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
  int sum = numbers.fold(0, (a, b) => a + b);
  int sum1 = numbers1.reduce((a, b) => a + b);
  print(sum);
  print(sum1);
}

The Output will be 55 and 66

6. every()

Confirms that every element satisfies the test



void main() {
  final checkage = name.every((e) => e.age >= 5);
  print(checkage.toString());
}

class Person {
  final String name;
  final int age;

  Person(this.name, this.age);
}

List<Person> name = [
  Person('A', 10),
  Person('B', 20),
  Person('C', 30),
  Person('D', 40),
  Person('E', 50),
  Person('F', 60),
];

The Output will be true

7. where(), firstWhere(), singleWhere()

Returns a collection of elements that satisfy a test.

firstWhere() returns the first match in the list,

singleWhere() returns the first match provided there is exactly one match.


void main() {
 List<int> numeber = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  numeber.where((e) => e % 2 == 0).forEach((e) => print(e));
  final s = numeber.firstWhere((element) => element.toString().contains('5'));
  final ss = numeber.singleWhere((element) => element == 4);
  print(s);
  print(ss);
 
}

The Output will be 2 4 6 8 10 and 5 and 4

8. take(), skip()

Returns a collection while including or skipping elements

void main() {
  List<String> names = [
    'Dell',
    'MAC',
    'OS',
  ];

  var data = names.skip(2).toList();
  var data1 = names.take(2).toList();
  print(data);
  print(data1);
}

The output will be [OS] and [Dell, MAC]

9.  List.from()

Creates a new list from the given collection

void main() {
  List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  var data = List.from(numbers);
  print(data);
}

The output will be [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

10.  expand()

Expands each element into zero or more elements

void main() {
  var input = [1, 2, 3, 4, 5];
  var data = input.expand((i) => [i, i]).toList();
  print(data);
}

The output will be [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]

Thank you for reading the Guide about Top 10 Array utility methods that We should know in Dart