Skip to main content

Command Palette

Search for a command to run...

Understanding Constants, Lists, and Maps in Dart (With Examples)

Updated
3 min read

Dart is a powerful programming language designed for building mobile, desktop, server, and web applications. It’s the backbone of Flutter, a popular framework for cross-platform app development. In this blog, we’ll explore constants, lists, and maps in Dart — three essential concepts every beginner should understand.


Constants in Dart

Constants are variables whose values never change. Dart provides two ways to declare constants:

  1. final: Used for variables whose values are set once and can’t be reassigned.

  2. const: Used for compile-time constants whose values are determined at compile-time.

Syntax

final variableName = value;
const variableName = value;

Example

void main() {
  final currentYear = 2024;
  const pi = 3.14159;

  print('Current Year: \$currentYear');
  print('Value of Pi: \$pi');
}

Key Differences Between final and const

  • final: The value is set once at runtime and cannot be changed thereafter.

  • const: The value must be a compile-time constant and is deeply immutable.


Lists in Dart

A list is an ordered collection of items. Dart lists are similar to arrays in other languages and come in two types:

  1. Fixed-length list: The length of the list is fixed and cannot change.

  2. Growable list: The list can dynamically grow or shrink in size.

Syntax

List<Type> listName = [value1, value2, ...];

Example: Fixed-Length List

void main() {
  var fixedList = List.filled(3, 0); // A fixed-length list of 3 elements
  fixedList[0] = 10;
  fixedList[1] = 20;
  fixedList[2] = 30;

  print(fixedList); // Output: [10, 20, 30]
}

Example: Growable List

void main() {
  var growableList = [1, 2, 3];
  growableList.add(4);
  growableList.remove(2);

  print(growableList); // Output: [1, 3, 4]
}

Common List Operations

  • Add an item: list.add(value)

  • Remove an item: list.remove(value)

  • Access an item: list[index]

  • Get the length: list.length


Maps in Dart

A map is an unordered collection of key-value pairs, where each key is unique, and each key maps to exactly one value. Maps are perfect for scenarios where data needs to be accessed via a unique key.

Syntax

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

Example

void main() {
  var capitals = {
    'India': 'New Delhi',
    'USA': 'Washington, D.C.',
    'Japan': 'Tokyo',
  };

  print(capitals['India']); // Output: New Delhi

  // Add a new key-value pair
  capitals['France'] = 'Paris';
  print(capitals);
}

Common Map Operations

  • Add or update a key-value pair: map[key] = value;

  • Remove a key-value pair: map.remove(key);

  • Check if a key exists: map.containsKey(key)

  • Check if a value exists: map.containsValue(value)

  • Get all keys: map.keys

  • Get all values: map.values


Combining Lists and Maps

Dart makes it easy to work with lists and maps together. For instance, you might have a list of maps to represent multiple objects:

Example: List of Maps

void main() {
  var students = [
    {'name': 'Alice', 'grade': 'A'},
    {'name': 'Bob', 'grade': 'B+'},
    {'name': 'Charlie', 'grade': 'A-'}
  ];

  for (var student in students) {
    print('Name: ${student['name']}, Grade: ${student['grade']}');
  }
}

Conclusion

Understanding constants, lists, and maps in Dart is crucial for managing data efficiently in your programs. With constants, you can ensure values don’t change unintentionally. With lists, you can manage ordered collections, and with maps, you can handle data in key-value pairs effectively. Mastering these will set a strong foundation for building robust applications in Dart and Flutter.

Happy Coding!

More from this blog

S

Suneel D.K

25 posts

Learn Flutter and Dart with easy tutorials and YouTube videos! Master app development with tips, tricks, and detailed guides for beginners and pros alike. Start building today!