Dart Lists

Working with ordered collections in Dart

📋 Understanding Dart Lists

Lists are ordered collections that can contain duplicate elements. They're indexed starting from 0 and provide methods for adding, removing, and accessing elements by position.


// Creating and using a simple list
List<String> fruits = ['apple', 'banana', 'orange'];
print(fruits[0]); // apple
fruits.add('grape');
                                    

List Features

🔢

Indexed Access

Access elements by position

list[0] // First element
list[list.length - 1] // Last element
📏

Dynamic Size

Grow and shrink as needed

list.add('new item');
list.remove('old item');
🔄

Allow Duplicates

Same value can appear multiple times

['apple', 'banana', 'apple'] // Valid
📊

Ordered

Elements maintain insertion order

list.insert(1, 'middle'); // Insert at index

🔹 Creating Lists

Different ways to create and initialize lists:

void main() {
  // Empty list
  List<String> emptyList = [];
  List<int> emptyNumbers = <int>[];
  
  // List with initial values
  List<String> fruits = ['apple', 'banana', 'orange'];
  var numbers = [1, 2, 3, 4, 5];
  
  // Fixed-length list
  List<int> fixedList = List.filled(3, 0); // [0, 0, 0]
  
  // Generated list
  List<int> squares = List.generate(5, (index) => index * index);
  
  print('Fruits: $fruits');
  print('Fixed list: $fixedList');
  print('Squares: $squares');
}

Output:

Fruits: [apple, banana, orange]

Fixed list: [0, 0, 0]

Squares: [0, 1, 4, 9, 16]

🔹 Accessing List Elements

Various ways to access and modify list elements:

void main() {
  List<String> colors = ['red', 'green', 'blue', 'yellow'];
  
  // Access by index
  print('First color: ${colors[0]}');
  print('Last color: ${colors[colors.length - 1]}');
  
  // Safe access methods
  print('First: ${colors.first}');
  print('Last: ${colors.last}');
  
  // Modify elements
  colors[1] = 'purple';
  print('After change: $colors');
  
  // Check bounds
  if (colors.length > 2) {
    print('Third color: ${colors[2]}');
  }
}

Output:

First color: red

Last color: yellow

First: red

Last: yellow

After change: [red, purple, blue, yellow]

Third color: blue

🔹 List Methods

Common methods for manipulating lists:

void main() {
  List<int> numbers = [1, 2, 3];
  
  // Adding elements
  numbers.add(4);                    // Add single element
  numbers.addAll([5, 6, 7]);        // Add multiple elements
  numbers.insert(0, 0);             // Insert at specific index
  
  print('After adding: $numbers');
  
  // Removing elements
  numbers.remove(7);                // Remove by value
  numbers.removeAt(0);              // Remove by index
  numbers.removeLast();             // Remove last element
  
  print('After removing: $numbers');
  
  // Other useful methods
  print('Contains 3: ${numbers.contains(3)}');
  print('Index of 4: ${numbers.indexOf(4)}');
  print('Reversed: ${numbers.reversed.toList()}');
}

Output:

After adding: [0, 1, 2, 3, 4, 5, 6, 7]

After removing: [1, 2, 3, 4, 5]

Contains 3: true

Index of 4: 3

Reversed: [5, 4, 3, 2, 1]

🔹 List Iteration

Different ways to loop through list elements:

void main() {
  List<String> animals = ['cat', 'dog', 'bird', 'fish'];
  
  // For-in loop
  print('Using for-in:');
  for (String animal in animals) {
    print('- $animal');
  }
  
  // Traditional for loop
  print('\nUsing traditional for:');
  for (int i = 0; i < animals.length; i++) {
    print('${i + 1}. ${animals[i]}');
  }
  
  // forEach method
  print('\nUsing forEach:');
  animals.forEach((animal) => print('* $animal'));
}

Output:

Using for-in:

- cat

- dog

- bird

- fish


Using traditional for:

1. cat

2. dog

3. bird

4. fish


Using forEach:

* cat

* dog

* bird

* fish

🧠 Test Your Knowledge

What is the index of the first element in a Dart list?