How to Create a List in Dart
In Dart, a List
is an ordered collection of items that can contain duplicate elements. Lists are indexed, meaning you can access elements by their position in the list. Dart provides several ways to create lists, and they can be mutable (modifiable) or immutable (fixed-length).
1. Creating a Mutable List
A mutable list can be modified after it is created. You can add, remove, or change elements in a mutable list.
Example of Creating a Mutable List
void main() {
// Creating a mutable list of integers
List<int> numbers = [1, 2, 3, 4, 5];
// Adding an element
numbers.add(6);
// Removing an element
numbers.remove(3);
// Accessing elements
print(numbers[0]); // Output: 1
// Iterating through the list
for (var number in numbers) {
print(number); // Output: 1 2 4 5 6
}
}
</int>
In this example, we create a mutable list of integers, add an element, remove an element, access an element by its index, and iterate through the list to print its contents.
2. Creating a Fixed-Length List
A fixed-length list cannot be modified after it is created. You cannot add or remove elements, but you can change the values of existing elements.
Example of Creating a Fixed-Length List
void main() {
// Creating a fixed-length list of integers
List<int> fixedNumbers = List.filled(5, 0); // List of length 5, initialized with 0
// Changing values
fixedNumbers[0] = 10;
fixedNumbers[1] = 20;
// Accessing elements
print(fixedNumbers[0]); // Output: 10
// Iterating through the fixed-length list
for (var number in fixedNumbers) {
print(number); // Output: 10 20 0 0 0
}
// Attempting to add an element will result in an error
// fixedNumbers.add(30); // Uncommenting this line will cause an error
}
</int>
In this example, we create a fixed-length list of integers initialized with zeros. We change some values and iterate through the list to print its contents. Attempting to add an element to a fixed-length list will result in an error.
3. Creating a List Using Constructors
You can also create a list using the List
constructor. This is useful when you want to create an empty list or a list with a specific length.
Example of Creating a List Using Constructors
void main() {
// Creating an empty list
List<string> emptyList = List<string>.empty();
// Creating a list with a specific length
List<string> stringList = List<string>.filled(3, 'default');
// Adding elements to the empty list
emptyList.add('Hello');
emptyList.add('Dart');
// Accessing elements
print(emptyList[0]); // Output: Hello
print(stringList); // Output: [default, default, default]
}
</string></string></string></string>
In this example, we create an empty list of strings and a list of strings with a specific length initialized with the value 'default'. We add elements to the empty list and print the contents of both lists.
4. Conclusion
Creating lists in Dart is straightforward and flexible. You can create mutable or fixed-length lists, use constructors, and manipulate the contents as needed. Understanding how to create and work with lists is essential for managing collections of data effectively in Dart applications.