Dart Map Common functions


In Dart, a Map is a collection of key-value pairs where each key is unique. It is useful for storing and accessing data where you want to associate values with specific keys. Dart provides several built-in functions (methods) for working with maps. Here’s an overview of common functions you can use with maps in Dart:

1. Creation and Initialization

  • Creating a Map:
    Map<String, int> ages = {}; // An empty map Map<String, int> fruits = { 'Apple': 100, 'Banana': 80, 'Orange': 90 }; // A map with initial key-value pairs

2. Adding Elements

  • addAll(): Adds all key-value pairs from another map.

    ages.addAll({'Alice': 30, 'Bob': 25}); // ages: {'Alice': 30, 'Bob': 25}
  • [] (Indexing): Adds or updates a key-value pair.

    ages['Charlie'] = 35; // ages: {'Alice': 30, 'Bob': 25, 'Charlie': 35}

3. Removing Elements

  • remove(): Removes a key-value pair by its key.

    ages.remove('Bob'); // ages: {'Alice': 30, 'Charlie': 35}
  • clear(): Removes all key-value pairs from the map.

    ages.clear(); // ages: {}

4. Accessing Elements

  • [] (Indexing): Accesses the value associated with a specific key.

    int aliceAge = ages['Alice']; // aliceAge: 30
  • keys: Returns an iterable of all keys in the map.

    Iterable<String> allKeys = ages.keys; // allKeys: ('Alice', 'Charlie')
  • values: Returns an iterable of all values in the map.

    Iterable<int> allValues = ages.values; // allValues: (30, 35)
  • containsKey(): Checks if a specific key exists in the map.

    bool hasAlice = ages.containsKey('Alice'); // hasAlice: true
  • containsValue(): Checks if a specific value exists in the map.

    bool hasTwentyFive = ages.containsValue(25); // hasTwentyFive: false

5. Iterating Through the Map

  • forEach(): Executes a function for each key-value pair in the map.

    ages.forEach((key, value) { print('$key: $value'); // Outputs each key-value pair });
  • map(): Applies a function to each key-value pair and returns a new map.

    Map<String, int> ageInFiveYears = ages.map((key, value) => MapEntry(key, value + 5)); // ageInFiveYears: {'Alice': 35, 'Charlie': 40}

6. Set Operations

  • update(): Updates the value associated with a specific key or adds a new key-value pair if the key does not exist.

    ages.update('Alice', (value) => value + 1, ifAbsent: () => 1); // ages: {'Alice': 31, 'Charlie': 35}
  • putIfAbsent(): Adds a key-value pair only if the key is not already in the map.

    ages.putIfAbsent('Dave', () => 28); // ages: {'Alice': 31, 'Charlie': 35, 'Dave': 28}

7. Conversion

  • toList(): Converts the map to a list of key-value pairs.
    List<MapEntry<String, int>> entries = ages.entries.toList(); // entries: [MapEntry('Alice', 31), MapEntry('Charlie', 35)]

Conclusion

Dart's Map collection provides a comprehensive set of functions that enable you to create, manipulate, and access collections of key-value pairs efficiently. Understanding these common functions allows you to manage data effectively in your applications, whether you need to add, remove, or access elements by their keys. Dart’s map methods simplify these tasks and enhance your coding experience, making it easier to handle associative data.