Top 10 Map/Object utility methods you should know (Dart) 🎯
Tags: dart, webdev, beginners, showdev
Published: 2018-07-23 06:58:55

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

In this article we will look at the top 10 utility methods you should know about the `Map` type in Dart. This is a sister article to the earlier "[Top 10](https://dev.to/graphicbeacon/top-10-array-utility-methods-you-should-know-dart-version--3217)" I did on Array/List types, so let's jump right into it.

---

## 1. addAll()

This allows you to merge all key/value pairs of the provided map to the current one.

```dart
var user = {
  "firstName": "Tom",
  "age": 25,
};

user.addAll({
  "lastName": "Smith",
  "age": 26,
});

print(user); // => {"firstName": "Tom", "age": 26, "lastName": "Smith"}
```
If a key already exists, its value will be replaced. *This works similarly to `Object.assign` in JavaScript.*

## 2. containsKey()

Checks whether the given key exists

```dart
print(user.containsKey("age")); // => true
print(user.containsKey("accessToken")); // => false
```

## 3. containsValue()

Checks whether the given value exists

```dart
print(user.containsValue("Smith")); // => true
print(user.containsValue(40)); // => false
```

## 4. forEach()

Runs the given function over each key/value pair

```dart
user.forEach((key, value) => print('Key: $key, Value: $value')); // => "Key: firstName, Value: Tom" "Key: age, Value: 26" "Key: lastName, Value: Smith"
```

## 5. putIfAbsent()

Adds a key/value pair if non-existent. If key already exists, a value will be set if there isn't one.

```dart
user.putIfAbsent("accessToken", () => "abf329jklr90rnlk2..."); // => {"firstName": "Tom", "age": 26, "lastName": "Smith", "accessToken": "abf329jklr90rnlk2..."}
```

## 6. remove()

Removes the provided key and its associated value

```dart
user.remove("accessToken"); // => abf329jklr90rnlk2...
```
This will return the value that was removed.

## 7. removeWhere()

Removes the key/value pair if the given condition is true

```dart
user.removeWhere((key, value) => key == "lastName");
```

## 8. clear()

Removes every key/value pair in the map

```dart
user.clear();
print(user); // => {}
```

## 9. update()

Updates the value for the given key

```dart
user["age"] = 25;
user.update("age", (dynamic val) => ++val); // => 26
```

This also returns the new value. To prevent an error being thrown should the key not exist, there's a third parameter:

```dart
user.update("name", (dynamic val) => "Jim", ifAbsent: () => "Jane");
print(user); // => {"age": 26, "name": "Jane"};
```

In most cases you could update using array bracket notation:

```dart
user["name"] = "Mary";
```

## 10. Map.from()

This technically is a constructor, but still useful as a utility. It creates and returns a copy of the provided map:

```dart
var userCopy = Map.from(user);
```

## Conclusion

I hope this was 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 code snippets for this article are available on [DartPad](https://dartpad.dartlang.org/b738e69b9985a5c11a841c94de1a1261).

**Like and follow me** 😍 for more articles on Dart. Thanks so much.

## Quicklinks

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

---

Continue reading:
{% link https://dev.to/graphicbeacon/top-10-string-utility-methods-you-should-know-dart--4df0 %}

---