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 List Methods


Share

List Methods and Properties

Dart lists have many built-in methods and properties.

This table shows the most common ones:

Method or propertyDescription
add()Adds an item to the end of the list
addAll()Adds all items of another collection to the end of the list
any()Returns true if at least one item passes a test
clear()Removes all items from the list
contains()Returns true if the list has the given value
every()Returns true if all items pass a test
firstThe first item of the list
fold()Combines all items into one value, starting from a value you give
forEach()Runs a function for each item
indexOf()Returns the index of the first matching item, or -1
insert()Adds an item at a given index
isEmptyTrue if the list has no items
isNotEmptyTrue if the list has at least one item
join()Joins all items into one string
lastThe last item of the list
lengthThe number of items in the list
map()Creates new values by running a function on each item
reduce()Combines all items into one value, starting from the first item
remove()Removes the first item with the given value
removeAt()Removes the item at a given index
removeLast()Removes the last item
removeWhere()Removes all items that pass a test
reversedThe items in reverse order
sort()Sorts the list
sublist()Returns a new list with a part of the items
toList()Creates a list from the items
where()Keeps only the items that pass a test

Sort a List

The sort() method sorts the list in place.

Numbers are sorted from low to high, and strings are sorted alphabetically:

Example

void main() {
  final numbers = [42, 7, 19, 3];
  numbers.sort();
  print(numbers);

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

Result:

[3, 7, 19, 42]
[apple, banana, cherry]

To sort in another order, give sort() a function that compares two items, a and b.

The compareTo() method returns a negative number, zero or a positive number, which tells sort() which item comes first.

Here we compare b with a, to sort from high to low:

Example

void main() {
  final scores = [72, 95, 58, 81];
  scores.sort((a, b) => b.compareTo(a));
  print(scores);
}
Try it Yourself »

Result:

[95, 81, 72, 58]


map() and where()

The map() method runs a function on each item and gives the new values.

The where() method keeps only the items that pass a test.

Both methods return an Iterable, a collection that you can loop through.

Use toList() to turn the result into a list:

Example

void main() {
  final prices = [10, 25, 40, 5];

  final doubled = prices.map((price) => price * 2).toList();
  print(doubled);

  final cheap = prices.where((price) => price < 20).toList();
  print(cheap);
}
Try it Yourself »

Result:

[20, 50, 80, 10]
[10, 5]

The original list does not change.


reduce() and fold()

The reduce() and fold() methods combine all the items into one value.

reduce() starts with the first item, and then combines it with each next item:

Example

void main() {
  final numbers = [3, 8, 2, 5];
  final sum = numbers.reduce((a, b) => a + b);
  print(sum);
}
Try it Yourself »

Result:

18

fold() starts with a value that you give.

Use fold() when the list can be empty, or when the result has another type than the items:

Example

void main() {
  final words = ['Dart', 'is', 'fun'];
  final letters = words.fold(0, (total, word) => total + word.length);
  print(letters);
}
Try it Yourself »

Result:

9

On an empty list, reduce() throws an error, because there is no first item:

Example

void main() {
  final numbers = <int>[];
  final total = numbers.fold(0, (a, b) => a + b);
  print(total);
  print(numbers.reduce((a, b) => a + b));
}
Try it Yourself »

Result:

0
Unhandled exception:
Bad state: No element

The fold() call works, and prints its start value 0.


join() and reversed

The join() method joins all items into one string, with the separator you give.

The reversed property gives the items in reverse order:

Example

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

Result:

apple, banana, cherry
apple and banana and cherry
[cherry, banana, apple]

any() and every()

The any() method returns true if at least one item passes the test.

The every() method returns true only if all items pass the test:

Example

void main() {
  final ages = [17, 22, 35, 41];
  print(ages.any((age) => age < 18));
  print(ages.every((age) => age >= 18));
}
Try it Yourself »

Result:

true
false


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.