Dart List


In Dart, a list is a collection of ordered elements that can be of any data type. Lists are one of the most commonly used data structures in Dart, allowing you to store multiple values in a single variable. They are mutable, meaning you can modify their contents after creation. Dart provides a rich set of features and methods for working with lists, making them versatile for various programming tasks.

Creating Lists

You can create lists in Dart using the List class. There are different ways to create a list:

  1. Using List Literals:
void main() { // Create a list of integers List<int> numbers = [1, 2, 3, 4, 5]; // Create a list of strings List<String> fruits = ['apple', 'banana', 'cherry']; print(numbers); // Output: [1, 2, 3, 4, 5] print(fruits); // Output: [apple, banana, cherry] }
  1. Using the List Constructor:

You can also create a list using the List constructor.

void main() { // Create an empty list List<int> emptyList = List<int>.empty(); // Create a list with a specific length List<int> fixedLengthList = List<int>.filled(5, 0); print(emptyList); // Output: [] print(fixedLengthList); // Output: [0, 0, 0, 0, 0] }

Accessing List Elements

You can access list elements using their index (0-based).

Example:

void main() { List<String> fruits = ['apple', 'banana', 'cherry']; // Access elements using their index print(fruits[0]); // Output: apple print(fruits[1]); // Output: banana print(fruits[2]); // Output: cherry }

Modifying Lists

Lists are mutable, allowing you to change their contents:

  • Adding Elements: Use add(), addAll(), or insert().
void main() { List<String> fruits = ['apple', 'banana']; // Adding a single element fruits.add('cherry'); // Adding multiple elements fruits.addAll(['date', 'elderberry']); // Inserting an element at a specific index fruits.insert(1, 'blueberry'); print(fruits); // Output: [apple, blueberry, banana, cherry, date, elderberry] }
  • Removing Elements: Use remove(), removeAt(), or clear().
void main() { List<String> fruits = ['apple', 'banana', 'cherry']; // Remove an element by value fruits.remove('banana'); // Removes 'banana' // Remove an element by index fruits.removeAt(0); // Removes 'apple' print(fruits); // Output: [cherry] }

List Properties and Methods

Dart provides various properties and methods to work with lists:

  • Length: Get the number of elements in the list.
void main() { List<String> fruits = ['apple', 'banana', 'cherry']; print(fruits.length); // Output: 3 }
  • Sorting: You can sort lists using the sort() method.
void main() { List<int> numbers = [5, 2, 9, 1, 5, 6]; numbers.sort(); print(numbers); // Output: [1, 2, 5, 5, 6, 9] }
  • Reversing: You can reverse the order of elements using the reversed property.
void main() { List<String> fruits = ['apple', 'banana', 'cherry']; List<String> reversedFruits = fruits.reversed.toList(); print(reversedFruits); // Output: [cherry, banana, apple] }
  • Filtering: Use the where() method to filter elements based on a condition.
void main() { List<int> numbers = [1, 2, 3, 4, 5, 6]; List<int> evenNumbers = numbers.where((number) => number.isEven).toList(); print(evenNumbers); // Output: [2, 4, 6] }

Iterating Over Lists

You can iterate over lists using various methods:

  1. Using a for Loop:
void main() { List<String> fruits = ['apple', 'banana', 'cherry']; for (int i = 0; i < fruits.length; i++) { print(fruits[i]); } }
  1. Using the forEach Method:
void main() { List<String> fruits = ['apple', 'banana', 'cherry']; fruits.forEach((fruit) { print(fruit); }); }
  1. Using a for-in Loop:
void main() { List<String> fruits = ['apple', 'banana', 'cherry']; for (var fruit in fruits) { print(fruit); } }

Conclusion

Lists in Dart are a powerful and flexible way to manage collections of data. They provide a rich set of methods for adding, removing, accessing, and manipulating elements, making them suitable for a wide variety of programming tasks. Understanding how to work with lists effectively is essential for developing Dart applications, whether you're handling simple data collections or implementing complex algorithms. By mastering lists, you can build efficient and organized code that can easily adapt to changing requirements.