Skip to main content
A List<E> is an indexable, ordered collection of objects that implements the Iterable interface. It serves as the standard array implementation in Dart, providing zero-based indexing and support for both fixed-length and growable configurations.

Type Definition

The List class is generic, defined as List<E>, where E represents the type of elements contained within the collection. Dart’s type inference system can automatically determine E based on the elements provided at initialization, or it can be explicitly declared.
// Explicit typing
List<int> integers = [1, 2, 3];

// Type inference (inferred as List<String>)
var strings = ['a', 'b', 'c'];

// Dynamic typing (heterogeneous list)
List<dynamic> mixed = [1, 'a', true];

List Classifications

Dart lists are categorized by their mutability and length constraints.

1. Growable List

The default implementation via array literals. The internal buffer resizes dynamically as elements are added or removed.
var growable = [1, 2];
growable.add(3);    // Valid: Length becomes 3
growable.remove(1); // Valid: Length becomes 2

2. Fixed-Length List

A list where the length is immutable after instantiation. The values at specific indices can be modified, but the structure (size) cannot. Attempting to add or remove elements throws an UnsupportedError.
// Creates a list of length 3, filled with null
var fixed = List<int?>.filled(3, null); 

fixed[0] = 5; // Valid: Modifying element
// fixed.add(4); // Throws UnsupportedError

3. Unmodifiable List

A list where both the length and the elements are immutable.
var immutable = List.unmodifiable([1, 2, 3]);
// immutable[0] = 5; // Throws UnsupportedError

Initialization Syntax

Literal Syntax

The most common method for creating lists using square brackets.
var list = [1, 2, 3];

Constructors

The List class provides named constructors for specific initialization patterns.
  • List.filled(int length, E fill): Creates a fixed-length list.
  • List.generate(int length, E generator(int index)): Generates values based on the index.
  • List.of(Iterable<E> elements): Creates a growable list from another iterable.
var generated = List.generate(3, (index) => index * 2); // [0, 2, 4]

Operators and Control Flow

Dart supports collection operators directly within list literals.

Spread Operator (... and ...?)

Inserts multiple elements from another collection into the list. The null-aware spread operator (...?) handles nullable iterables.
var listA = [1, 2];
var listB = [0, ...listA, 3]; // [0, 1, 2, 3]

Collection If and For

Allows conditional inclusion or iteration during list construction.
bool include = true;
var list = [
  1,
  if (include) 2,
  for (var i = 3; i < 5; i++) i
]; // [1, 2, 3, 4]

Element Access and Mutation

Elements are accessed via the subscript operator [] using a zero-based index.
var list = ['A', 'B', 'C'];

// Read
print(list[0]); // Output: A

// Write
list[1] = 'D'; // list is now ['A', 'D', 'C']

// Properties
print(list.first); // 'A'
print(list.last);  // 'C'
print(list.length); // 3
print(list.isEmpty); // false

Null Safety

Null safety rules apply to both the list structure and the elements within it.
  • List<String>: A non-null list containing non-null strings.
  • List<String?>: A non-null list containing strings that may be null.
  • List<String>?: The list itself may be null, but if it exists, it contains non-null strings.
  • List<String?>?: The list may be null, and elements may be null.
Master Dart with Deep Grasping Methodology!Learn More