Skip to main content

Command Palette

Search for a command to run...

Dart Collections: Dart Map

Updated
1 min read
Dart Collections: Dart Map
V

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation.

In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology.

Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities.

In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"

In Dart, a Map is a collection that allows you to associate keys with values. Think of it like a real-world dictionary where you look up a word (key) to find its definition (value). In Dart, keys and values can be of any type.

Here's a simple explanation of how to use a Map in Dart:

void main() {
  // Creating a Map
  Map<String, int> ages = {
    'Alice': 25,
    'Bob': 30,
    'Charlie': 22,
  };

  // Accessing values using keys
  print("Alice's age: ${ages['Alice']}");  // Output: Alice's age: 25

  // Adding a new key-value pair
  ages['David'] = 28;

  // Updating the value for an existing key
  ages['Alice'] = 26;

  // Removing a key-value pair
  ages.remove('Bob');

  // Iterating through the Map
  ages.forEach((key, value) {
    print("$key's age: $value");
  });
}

In this example:

  • We create a Map named ages where keys are String (names) and values are int (ages).

  • We access values using keys, update values, add new key-value pairs, and remove a key-value pair.

  • We use forEach to iterate through the Map and print each key-value pair.

In Dart, Map provides a powerful way to organize and retrieve data using keys. Keys are unique within a Map, and they help you efficiently look up associated values.

Learn Dart

Part 1 of 50

Dive into the Dart Language Series for an easy grasp of Dart programming essentials.

More from this blog

Vinit Mepani (Flutter Developer)

270 posts

"Vinit Mepani, passionate coder! Dive into my Dart and Flutter journey on the blog. Let's master these tech wonders together. Happy coding! 🚀"