Dart's Collection: List
Explore how to use Dart lists to manage multiple related items in a specific order. Learn to create lists with type inference or explicit types, access elements using zero-based indexing, and safely add or remove items. Understand how to transform list data with map() while preserving the original list. This lesson builds foundational skills in handling Dart collections essential for scalable app development.
We'll cover the following...
As our programs grow, we need ways to manage multiple pieces of related data. Dart collections are objects that group other objects together. Dart provides three core built-in collection types:
List: An ordered collection of items accessed by an index.
Set: An unordered collection of unique items.
Map: An unordered collection of key-value pairs.
We will focus on the most common collection first: the List.
Understanding lists
In programming, we often need to store a sequence of items, such as a list of names or a series of temperature readings. A List is a collection that stores items in a specific, ordered sequence.
Creating a list
In programming, we often need to store a sequence of items, such as a list of names or a series of temperature readings. A Dart list is a collection that stores items in a specific, ordered sequence.
We create a list using square brackets []. When we place items inside these brackets, Dart automatically infers the Dart types based on the data you provide. This is known as a list literal.
If we want to create an empty list to add items to later, we cannot rely on type inference because there are no items for Dart to inspect. We must explicitly define the data type using angle brackets <Type> before the square brackets.
Line 2: We declare a list of integers using a list literal. Dart automatically infers the type as a list of integers.
Line 3: We create an empty list. We explicitly mark it as a list that will hold strings using
<String>[]so Dart knows exactly what data this list is allowed to store.
Zero-based indexing
Because a list is ordered, every item inside it is assigned a fixed numerical position called an index. Dart uses zero-based indexing. This means the counting does not start at 1. The very first element in a list is always at index 0, the second element is at index 1, and so on.
To find the index of the final element, we take the total number of items in the list and subtract one. We use square brackets [] containing the index number to access specific items directly.
Let us look at a code example that accesses the first and last elements of a list using their index positions.
Line 2: We create a list containing three strings.
Line 4: We print the
lengthproperty, which outputs 3 because there are exactly three items inside the list.Line 5: We access the first element by passing index 0 inside the square brackets. This outputs 'red'.
Line 6: We access the final element by subtracting 1 from the total length. Since the length is 3, this evaluates to index 2. Passing index 2 into the square brackets outputs 'blue'.
Manipulating lists safely
Because lists are objects, they contain built-in methods that allow us to add, remove, and manage items dynamically. We use the add() method to append a single item to the end of a list, and the addAll() method to append multiple items at once.
To remove an item by its specific position, we use the removeAt() method. When removing items, we must be careful to only provide an index that actually exists in the list. Attempting to access or remove an item at an invalid index causes the program to crash. For example, trying to remove the item at index 5 from our colors list will fail because the maximum index is 2 (previous example).
Lines 4—5: We expand our list by adding a single string (
'orange'), followed by adding a list of two new strings. Our list now has five items, occupying indices 0 through 4.Line 6: We first print the list before removing any elements from it.
Line 7: Because we know our list has five items, we can safely remove the item located at index 2 (
'orange').Line 8: We print the modified list, which outputs
[apple, banana, grape, mango].
Transforming lists
We often need to apply an operation to every item in a list to generate new data. Dart provides the map() method to handle this efficiently.
The map() method does not change the original list. It applies a function to each element and returns a completely new collection called an Iterable. Because an Iterable is a generalized collection type, we must attach the .toList() method to the end of our operation to convert the result back into a standard list.
Line 4: We call
map()on our list and pass a function that multiplies each number by itself three times. We immediately append.toList()to convert the resulting Iterable back into a standard list, storing the final result in a new variable.Lines 6—7: We output both variables to the console. The original list remains unaffected, while the new list contains the cubed values.
Mastering lists is an essential part of Dart basics that gives us the foundational skills to store, sequence, and transform data in our programs. By understanding how to safely navigate zero-based indices and manipulate ordered elements, we are fully prepared to explore Dart's other powerful collections in the upcoming lessons.