Maps in Dart

Dart Maps - A Complete Beginner Guide with Examples

Map in Dart - Beginner's Complete Guide with Examples

If you are learning Dart (or Flutter), understanding Maps is crucial. Maps help us store data as key-value pairs, just like dictionaries in Python or HashMaps in Java.

What is a Map in Dart?

A Map in Dart is an object that associates keys to values. Both keys and values can be of any type (String, int, double, etc.).

Basic Syntax:

Map<KeyType, ValueType> mapName = { key1: value1, key2: value2, };

Creating a Map in Dart

Example 1: Simple String Map

void main() { Map<String, String> countryCapital = { 'India': 'New Delhi', 'USA': 'Washington DC', 'Japan': 'Tokyo', }; print(countryCapital); }

Output:

{India: New Delhi, USA: Washington DC, Japan: Tokyo}

Accessing Map Values

You can access a value using its key.

void main() { Map<String, String> countryCapital = { 'India': 'New Delhi', 'USA': 'Washington DC', 'Japan': 'Tokyo', }; print(countryCapital['India']); // Output: New Delhi }

Adding and Updating Values in Map

You can add a new key-value pair or update an existing one like this:

void main() { Map<String, String> countryCapital = { 'India': 'New Delhi', 'USA': 'Washington DC', }; // Adding countryCapital['Japan'] = 'Tokyo'; // Updating countryCapital['USA'] = 'New Washington'; print(countryCapital); }

Output:

{India: New Delhi, USA: New Washington, Japan: Tokyo}

Checking if a Key Exists

void main() { Map<String, String> countryCapital = { 'India': 'New Delhi', 'USA': 'Washington DC', }; print(countryCapital.containsKey('India')); // true print(countryCapital.containsKey('France')); // false }

Iterating Over a Map

Example: Using forEach()

void main() { Map<String, String> countryCapital = { 'India': 'New Delhi', 'USA': 'Washington DC', 'Japan': 'Tokyo', }; countryCapital.forEach((key, value) { print('Country: $key, Capital: $value'); }); }

Removing Elements from Map

void main() { Map<String, String> countryCapital = { 'India': 'New Delhi', 'USA': 'Washington DC', 'Japan': 'Tokyo', }; countryCapital.remove('USA'); print(countryCapital); }

Map Properties and Methods

  • .length - Number of key-value pairs
  • .keys - All keys
  • .values - All values
  • .isEmpty - Check if empty
  • .isNotEmpty - Check if not empty

Example:

void main() { Map<String, String> countryCapital = { 'India': 'New Delhi', 'USA': 'Washington DC', }; print(countryCapital.length); // 2 print(countryCapital.keys); // (India, USA) print(countryCapital.values); // (New Delhi, Washington DC) print(countryCapital.isEmpty); // false }

Maps with Different Data Types

Map with Integer Keys and String Values:

void main() { Map<int, String> studentNames = { 1: 'Rahul', 2: 'Sneha', 3: 'Arjun', }; print(studentNames); }

Using Map in Flutter

Maps are widely used in Flutter for things like sending data between screens, API responses, and storing widget properties.

Example: Passing data using Map

Navigator.pushNamed( context, '/details', arguments: {'id': 101, 'name': 'Product A'}, );

Common Interview Questions on Dart Map

  • What is a Map in Dart?
  • How do you iterate through a Map?
  • Difference between Map and List?
  • How to convert Map to JSON?

To Do Assignments

  • Create a Map of Roll Number and Student Names.
  • Write a Dart program to count word frequency in a given string using Map.
  • Make a program to check if a value exists inside the Map.
  • Create a program that removes all keys whose values start with a particular letter.

Conclusion

Maps are super useful when you want to store and retrieve data in key-value form in Dart and Flutter. Understanding how to manipulate Maps makes your Dart coding journey much easier!

Related Topics:

Tip: Want to master Dart faster? Practice by solving small programs using Maps like phone books, product catalogs, or student records.

Happy Coding!

Post a Comment

0 Comments

Me