Lists in Dart

Lists are one of the most commonly used data structures in Dart. Think of a list as a container that holds multiple items in a specific order - like a shopping list or a playlist of songs. In Dart, lists are ordered collections of objects.

Creating Lists

There are several ways to create lists in Dart:

Empty Lists

// Creating an empty list of integers
List<int> numbers = [];

// Creating an empty list of strings
List<String> names = <String>[];

// Using List constructor
List<double> prices = List<double>.empty(growable: true);

Lists with Initial Values

// List of integers
List<int> scores = [85, 92, 78, 96, 88];

// List of strings
List<String> fruits = ['apple', 'banana', 'orange', 'mango'];

// Mixed type list (not recommended, but possible)
List<dynamic> mixedList = [1, 'hello', true, 3.14];

Accessing List Elements

Lists use zero-based indexing, meaning the first element is at index 0:

List<String> colors = ['red', 'green', 'blue', 'yellow'];

print(colors[0]);  // Output: red
print(colors[2]);  // Output: blue
print(colors.first);  // Output: red (first element)
print(colors.last);   // Output: yellow (last element)

Common List Properties

PropertyDescriptionExample
lengthReturns the number of elementscolors.length
isEmptyReturns true if list is emptycolors.isEmpty
isNotEmptyReturns true if list is not emptycolors.isNotEmpty
firstReturns the first elementcolors.first
lastReturns the last elementcolors.last

Adding Elements

List<String> hobbies = ['reading', 'swimming'];

// Add single element at the end
hobbies.add('cycling');
print(hobbies); // [reading, swimming, cycling]

// Add multiple elements
hobbies.addAll(['painting', 'cooking']);
print(hobbies); // [reading, swimming, cycling, painting, cooking]

// Insert at specific position
hobbies.insert(1, 'dancing');
print(hobbies); // [reading, dancing, swimming, cycling, painting, cooking]

Removing Elements

List<int> numbers = [10, 20, 30, 40, 50];

// Remove specific value
numbers.remove(30);
print(numbers); // [10, 20, 40, 50]

// Remove at specific index
numbers.removeAt(0);
print(numbers); // [20, 40, 50]

// Remove last element
numbers.removeLast();
print(numbers); // [20, 40]

// Clear all elements
numbers.clear();
print(numbers); // []

Iterating Through Lists

Using for-in loop

List<String> animals = ['cat', 'dog', 'elephant', 'lion'];

for (String animal in animals) {
  print('I like $animal');
}

Using traditional for loop

for (int i = 0; i < animals.length; i++) {
  print('${i + 1}. ${animals[i]}');
}

Using forEach method

animals.forEach((animal) => print(animal.toUpperCase()));

List Methods

Here are some useful list methods:

List<int> numbers = [3, 1, 4, 1, 5, 9, 2, 6];

// Check if list contains an element
bool hasThree = numbers.contains(3); // true

// Find index of an element
int index = numbers.indexOf(1); // 1 (first occurrence)

// Sort the list
numbers.sort();
print(numbers); // [1, 1, 2, 3, 4, 5, 6, 9]

// Reverse the list
List<int> reversed = numbers.reversed.toList();
print(reversed); // [9, 6, 5, 4, 3, 2, 1, 1]

Fixed-Length vs Growable Lists

// Growable list (default)
List<String> growableList = ['a', 'b', 'c'];
growableList.add('d'); // This works

// Fixed-length list
List<String> fixedList = List.filled(3, 'x');
print(fixedList); // [x, x, x]
// fixedList.add('y'); // This would cause an error!

// You can still modify elements in fixed-length lists
fixedList[0] = 'hello';
print(fixedList); // [hello, x, x]

Lists are incredibly versatile and you'll use them frequently in your Dart programs. They're perfect for storing collections of related data that you need to access, modify, or iterate through!