HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to DartVariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Map
🔀 Condition & Loop
🧩 Function
Dart›Lists

Lists

List creation, element access, appending, length, and iterating with a loop.

Simple enough for a 5-year-old to understand

Create and access a list

A list holds multiple sequential values. The index starts at 0.

Example code
1
void main() {
2
var languages = ["Dart", "Go", "Rust"];
A list with three elements.
VariableA list with three elements.
3
print(languages[0]);
Dart (first element, index 0)
Output / InputDart (first element, index 0)
4
print(languages.length);
3 (number of elements)
Output / Input3 (number of elements)
5
print(languages);
[Dart, Go, Rust]
Output / Input[Dart, Go, Rust]
6
}
VariableOutput / Input
💡 Hint: First element items[0], last element items[length-1].

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.How do you get the first element in the list ["red", "purple"]?

About this topic

In Dart, ordered data is stored with List. Lists can be typed: an int list only accepts integers. If you don't specify a type, a dynamic list is created and you lose the benefit of type checking.

Lists can be in two forms: growable and fixed-length. A list created with square brackets can grow; a list created by filling it with a specific length is fixed and does not allow adding elements.

Indices start from zero, and going out of bounds produces a runtime error. Dart does not return null in this case; it makes the error visible immediately. This is better than code that fails silently.

What you will learn

  • Creating lists and accessing elements
  • Adding and removing elements
  • The concept of fixed-length lists
  • Iterating over a list
  • Transformation and filtering methods

Who is this for?

  • Those who will store data with a list
  • Those who get out-of-bounds errors
  • Developers who will build list screens in Flutter

When to use?

Behind scrollable lists in Flutter, there is always a Dart list; the number of rows is the length of the list.

Set is more suitable for unique values, and Map is more suitable for key-value mapping; putting every piece of data in a list increases the search cost.

Common mistakes

Wrong

void main() {
  var liste = List<int>.filled(3, 0);
  liste.add(4);
}

A list created in this format is fixed-length and does not allow adding elements, resulting in the "Unsupported operation: Cannot add to a fixed-length list" error. If you want a growable list, you must specify this explicitly.

Correct

void main() {
  var liste = List<int>.filled(3, 0, growable: true);
  liste.add(4);
}

Wrong

void main() {
  var liste = [1, 2, 3];
  for (var i = 0; i <= liste.length; i++) {
    print(liste[i]);
  }
}

Since less-than-or-equal is used in the loop condition, an invalid index is accessed in the final step, resulting in "RangeError: Index out of range". Valid indices range from zero to one less than the length; less-than should be used in the condition.

Correct

void main() {
  var liste = [1, 2, 3];
  for (var eleman in liste) {
    print(eleman);
  }
}

Wrong

void main() {
  var liste = [1, 2, 3, 4];
  for (var eleman in liste) {
    if (eleman.isEven) liste.remove(eleman);
  }
}

Deleting elements while iterating over a list produces a "Concurrent modification during iteration" error. The collection is not allowed to change during iteration. The correct way is to use the method that directly removes the matching items.

Correct

void main() {
  var liste = [1, 2, 3, 4];
  liste.removeWhere((eleman) => eleman.isEven);
}

Frequently asked questions

How do I find the list length?

The length property gives the number of elements. Using isEmpty or isNotEmpty to check if a list is empty is more readable than comparing the length to zero.

What do map and where do?

map transforms each element, and where selects the ones matching a condition. Both return lazily evaluated results; if you need a list, you must call toList at the end. This detail often surprises beginners.

How do I merge two lists?

You can produce a new list with the plus operator or add to an existing list with addAll. The three-dot spread operator also allows you to unpack the elements of another list inside a list literal; it is heavily used in Flutter code.

Summary

Lists are zero-indexed.

Out-of-bounds access produces a runtime error.

A list created with a specific length is fixed.

Deleting elements while iterating throws an error.

toList is required for the result of map and where.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code