Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Lists


Share

Dart Lists

A list stores many values in one variable.

The values in a list are called items, or elements.

To create a list, write the items in square brackets [], separated by commas:

Example

void main() {
  final fruits = ['apple', 'banana', 'cherry'];
  print(fruits);
}
Try it Yourself »

Result:

[apple, banana, cherry]

A list keeps its items in order, and it can have the same value more than once:

Example

void main() {
  final scores = [10, 20, 10, 30];
  print(scores);
}
Try it Yourself »

Result:

[10, 20, 10, 30]

Note: Many other languages call this an array.

In Dart, it is called a list.


Typed Lists

Every list has a type for its items.

In the examples above, Dart finds the type by itself from the values.

You can also write the type yourself, with List<Type>:

Example

void main() {
  List<String> cars = ['Volvo', 'BMW', 'Ford'];
  List<int> ages = [25, 31, 47];
  print(cars);
  print(ages);
}
Try it Yourself »

Result:

[Volvo, BMW, Ford]
[25, 31, 47]

A list of type List<String> can only hold strings.

If you try to add a number, the program does not run:

Example

void main() {
  List<String> cars = ['Volvo', 'BMW', 'Ford'];
  cars.add(42);
  print(cars);
}
Try it Yourself »

Result:

main.dart:3:12: Error: The argument type 'int' can't be assigned to the parameter type 'String'.
  cars.add(42);
           ^

To create an empty list, write the type before the brackets:

Example

void main() {
  final names = <String>[];
  print(names);
  names.add('Liam');
  print(names);
}
Try it Yourself »

Result:

[]
[Liam]

The add() method puts a new item at the end of the list.

You will learn more about it in the Change List Items chapter.



List Length

The length property gives the number of items in a list.

The isEmpty and isNotEmpty properties tell if the list has any items:

Example

void main() {
  final fruits = ['apple', 'banana', 'cherry'];
  final empty = <int>[];
  print(fruits.length);
  print(fruits.isEmpty);
  print(fruits.isNotEmpty);
  print(empty.length);
  print(empty.isEmpty);
}
Try it Yourself »

Result:

3
false
true
0
true

Growable and Fixed-Length Lists

The lists you create with [] are growable.

This means you can add and remove items, so the length can change.

A fixed-length list always has the same number of items.

You can create one with List.filled(), which takes the length and a start value for every item:

Example

void main() {
  final seats = List.filled(4, 'free');
  print(seats);
  seats[1] = 'taken';
  print(seats);
}
Try it Yourself »

Result:

[free, free, free, free]
[free, taken, free, free]

You can change the items of a fixed-length list, but you cannot add or remove items.

If you try, Dart throws an error when the program runs:

Example

void main() {
  final seats = List.filled(4, 'free');
  seats.add('free');
}
Try it Yourself »

Result:

Unhandled exception:
Unsupported operation: Cannot add to a fixed-length list

Constant and Unmodifiable Lists

A list created with const can never change.

You cannot add, remove or change its items:

Example

void main() {
  const colors = ['red', 'green', 'blue'];
  print(colors);
  colors.add('yellow');
}
Try it Yourself »

Result:

[red, green, blue]
Unhandled exception:
Unsupported operation: Cannot add to an unmodifiable list

The value of a const list must be known when the program is compiled.

If the values are only known when the program runs, use List.unmodifiable() to create a copy that cannot change.

Remember: final and const do not mean the same thing for lists.

A final variable cannot point to a new list, but you can still change the items of the list.

A const list cannot change at all.

This example adds an item to a final list:

Example

void main() {
  final fruits = ['apple', 'banana'];
  fruits.add('cherry');
  print(fruits);
}
Try it Yourself »

Result:

[apple, banana, cherry]


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.