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 Access List Items


Share

Access List Items

Each item in a list has a position number, called its index.

The first item has index 0, the second item has index 1, and so on.

To get an item, write the index in square brackets after the list name:

Example

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

Result:

apple
cherry

The last item always has the index length - 1:

Example

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

Result:

cherry

First and Last

The first and last properties give the first and the last item of a list:

Example

void main() {
  final cars = ['Volvo', 'BMW', 'Ford', 'Mazda'];
  print(cars.first);
  print(cars.last);
}
Try it Yourself »

Result:

Volvo
Mazda


Index Out of Range

If you use an index that does not exist in the list, Dart throws a RangeError when the program runs.

This list has 3 items, so the valid indexes are 0, 1 and 2:

Example

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

Result:

Unhandled exception:
RangeError (length): Invalid value: Not in inclusive range 0..2: 3

Note: Dart does not support negative indexes like fruits[-1].

Use last to get the last item.


Part of a List

The sublist() method gives a new list with a part of the items.

It takes a start index and an end index.

The item at the start index is included, but the item at the end index is not:

Example

void main() {
  final fruits = ['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon'];
  print(fruits.sublist(2, 5));
}
Try it Yourself »

Result:

[cherry, orange, kiwi]

If you leave out the end index, the new list goes to the end of the list:

Example

void main() {
  final fruits = ['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon'];
  print(fruits.sublist(3));
}
Try it Yourself »

Result:

[orange, kiwi, melon]

The original list does not change.


Find an Item

The contains() method returns true if the list has the item, and false if it does not:

Example

void main() {
  final fruits = ['apple', 'banana', 'cherry'];
  if (fruits.contains('apple')) {
    print('Yes, apple is in the list');
  }
  print(fruits.contains('mango'));
}
Try it Yourself »

Result:

Yes, apple is in the list
false

The indexOf() method returns the index of the first matching item.

If the item is not in the list, it returns -1:

Example

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

Result:

1
-1

The lastIndexOf() method works the same way, but it searches from the end:

Example

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

Result:

3


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.