free geoip
51

Dart Lists and Maps for Beginners Guide

Understanding Dart Lists and Maps is essential for every beginner learning the Dart programming language. Lists are used to store…

Understanding Dart Lists and Maps is essential for every beginner learning the Dart programming language. Lists are used to store ordered collections of data, while Maps store key-value pairs. Both are foundational when working with data structures in Dart, especially in mobile app development using Flutter.

Dart Lists and Maps

🔹 Dart Lists

A List in Dart is an ordered collection of items. Dart provides flexible methods to manipulate Lists such as .add(), .remove(), .insert(), etc.

Example:

void main() {
  List<String> fruits = ['Apple', 'Banana', 'Mango'];
  fruits.add('Orange');
  print(fruits); // Output: [Apple, Banana, Mango, Orange]

  fruits.remove('Banana');
  print(fruits); // Output: [Apple, Mango, Orange]
}

🔹 Dart Maps

Maps are a collection of key-value pairs. You can access, add, and remove entries using keys.

Example:

void main() {
  Map<String, int> scores = {
    'Alice': 90,
    'Bob': 85,
    'Charlie': 95
  };

  scores['David'] = 88; // Add new entry
  print(scores['Alice']); // Output: 90

  scores.remove('Bob');
  print(scores); // Output: {Alice: 90, Charlie: 95, David: 88}
}

These data structures are highly optimized and easy to use, making Dart a great choice for building mobile and web applications. You can read more on Dart’s official documentation here.

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *