Top 10 Array utility methods you should know (Dart) 🎯
Tags: dart, webdev, beginners, showdev
Published: 2018-06-28 23:44:41

---
title: Top 10 Array utility methods you should know (Dart) 🎯
published: true
description: Learn how to work with arrays in Dart
tags: dart, webdev, beginners, showdev
cover_image: https://thepracticaldev.s3.amazonaws.com/i/n3p7ycl62xuq9rdk5gbq.png
canonical_url: https://creativebracket.com/top-10-array-methods-you-should-know/
---

As part of my venture in client-side application development with [Dart](https://dartlang.org), I began exploring how one could go about working with Array/List types. Aside from the documentation being comprehensive, I was also able to find support on the [StackOverflow](https://stackoverflow.com/questions/tagged/dart) community and successfully achieved what was needed.

In today’s article we’ll be looking at the “batteries-included” notion of Dart, in particular, the [inbuilt utilities](https://api.dartlang.org/dev/2.0.0-dev.65.0/dart-core/List-class.html) for working with Lists. I’ve hand-picked 10 of the most common ones you show know for your next app. I’ve also [prepared](https://dartpad.dartlang.org/aec9a0ecd000063f53630799bb24b05c) the example snippets so you could play with those yourself 😁

So, shall we begin?

---

## 1. forEach()

Runs a function on each element in the list

```dart
var fruits = ['banana', 'pineapple', 'watermelon'];
fruits.forEach((fruit) => print(fruit)); // => banana pineapple watermelon
```

## 2. map()

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

```dart
var mappedFruits = fruits.map((fruit) => 'I love $fruit').toList();
print(mappedFruits); // => ['I love banana', 'I love pineapple', 'I love watermelon']
```

## 3. contains()

Checks to confirm that the given element is in the list

```dart
var numbers = [1, 3, 2, 5, 4];
print(numbers.contains(2)); // => true
```

## 4. sort()

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

```dart
numbers.sort((num1, num2) => num1 - num2); // => [1, 2, 3, 4, 5]
```

## 5. reduce(), fold()

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

```dart
var sum = numbers.reduce((current, next) => current + next);
print(sum); // => 15

// with fold() you provide an initial value
const initialValue = 10;
var sum2 = numbers.fold(initialValue, (current, next) => current + next);
print(sum2); // => 25
```

## 6. every()

Confirms that every element satisfies the test

```dart
List<Map<String, dynamic>> users = [
  { "name": 'John', "age": 18 },
  { "name": 'Jane', "age": 21 },
  { "name": 'Mary', "age": 23 },
];

var is18AndOver = users.every((user) => user["age"] >= 18);
print(is18AndOver); // => true
  
var hasNamesWithJ = users.every((user) => user["name"].startsWith('J'));
print(hasNamesWithJ); // => false
```

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

Returns a collection of elements that satisfy a test

```dart
// See #6 for users list
var over21s = users.where((user) => user["age"] > 21);
print(over21s.length); // => 1

var nameJ = users.firstWhere((user) => user["name"].startsWith('J'), orElse: () => null);
print(nameJ); // => {name: John, age: 18}

var under18s = users.singleWhere((user) => user["age"] < 18, orElse: () => null);
print(under18s); // => null
```

`firstWhere()` returns the first match in the list, while `singleWhere()` returns the first match provided there is **exactly one match**.

## 8. take(), skip()

Returns a collection while including or skipping elements

```dart
var fiboNumbers = [1, 2, 3, 5, 8, 13, 21];
print(fiboNumbers.take(3).toList()); // => [1, 2, 3]
print(fiboNumbers.skip(5).toList()); // => [13, 21]
print(fiboNumbers.take(3).skip(2).take(1).toList()); // => [3]
```

## 9. List.from()

Creates a new list from the given collection

```dart
var clonedFiboNumbers = List.from(fiboNumbers);
print('Cloned list: $clonedFiboNumbers');
```

*As of Dart 2.0, the* `new` *keyword is optional when instantiating objects.*

## 10. expand()

Expands each element into zero or more elements

```dart
var pairs = [[1, 2], [3, 4]];
var flattened = pairs.expand((pair) => pair).toList();
print('Flattened result: $flattened'); // => [1, 2, 3, 4]

var input = [1, 2, 3];
var duplicated = input.expand((i) => [i, i]).toList();
print(duplicated); // => [1, 1, 2, 2, 3, 3]
```


## Conclusion

I hope this has been insightful and if this is your first exposure to Dart, read my [first steps tutorial](https://dev.to/graphicbeacon/learn-dart-before-you-flutter-2997) to grasp the basics. The example snippets for this article are available on [DartPad](https://dartpad.dartlang.org/aec9a0ecd000063f53630799bb24b05c).

**Like and follow me**😍 for more content on Dart.

And this concludes the tutorial. Thanks for reading.

## What to check out next

1. [List\<E\> class Documentation](https://api.dartlang.org/dev/2.0.0-dev.65.0/dart-core/List-class.html)
2. **[Dart screencasts on Egghead.io](https://egghead.io/instructors/jermaine-oppong)**

---

Continue reading:
{% link https://dev.to/graphicbeacon/top-10-mapobject-utility-methods-you-should-know-dart--1ido %}

---