Dart List Common functions


In Dart, List is a versatile and commonly used collection type that represents an ordered collection of items. Dart provides a variety of built-in functions (methods) to manipulate lists effectively. Here are some common functions you can use with lists in Dart:

1. Creation and Initialization

  • Creating a List:
    List<int> numbers = []; // An empty list List<String> fruits = ['Apple', 'Banana', 'Orange']; // A list with initial elements

2. Adding Elements

  • add(): Adds an element to the end of the list.

    numbers.add(1); // numbers: [1]
  • addAll(): Adds multiple elements to the end of the list.

    numbers.addAll([2, 3, 4]); // numbers: [1, 2, 3, 4]
  • insert(): Inserts an element at a specified index.

    numbers.insert(1, 5); // numbers: [1, 5, 2, 3, 4]
  • insertAll(): Inserts multiple elements at a specified index.

    numbers.insertAll(2, [6, 7]); // numbers: [1, 5, 6, 7, 2, 3, 4]

3. Removing Elements

  • remove(): Removes the first occurrence of a specified element.

    numbers.remove(2); // numbers: [1, 5, 6, 7, 3, 4]
  • removeAt(): Removes the element at the specified index.

    numbers.removeAt(3); // numbers: [1, 5, 6, 3, 4]
  • removeLast(): Removes the last element from the list.

    numbers.removeLast(); // numbers: [1, 5, 6, 3]
  • removeRange(): Removes a range of elements from the list.

    numbers.removeRange(1, 3); // numbers: [1, 3]

4. Accessing Elements

  • [] (Indexing): Accesses an element at a specified index.

    int firstNumber = numbers[0]; // firstNumber: 1
  • first: Returns the first element of the list.

    var firstFruit = fruits.first; // firstFruit: 'Apple'
  • last: Returns the last element of the list.

    var lastFruit = fruits.last; // lastFruit: 'Orange'
  • length: Returns the number of elements in the list.

    int length = numbers.length; // length: 4

5. Checking Existence

  • contains(): Checks if the list contains a specific element.

    bool hasFive = numbers.contains(5); // hasFive: true
  • isEmpty: Checks if the list is empty.

    bool emptyCheck = numbers.isEmpty; // emptyCheck: false
  • isNotEmpty: Checks if the list is not empty.

    bool notEmptyCheck = numbers.isNotEmpty; // notEmptyCheck: true

6. Iterating Through the List

  • forEach(): Executes a function for each element in the list.

    numbers.forEach((number) { print(number); // Outputs each number in the list });
  • map(): Applies a function to each element and returns a new list.

    List<int> squaredNumbers = numbers.map((n) => n * n).toList(); // squaredNumbers: [1, 25, 36, 9]

7. Sorting and Reversing

  • sort(): Sorts the list in ascending order.

    List<int> unsortedNumbers = [5, 3, 8, 1]; unsortedNumbers.sort(); // unsortedNumbers: [1, 3, 5, 8]
  • reversed: Returns an iterable with the elements in reverse order.

    var reversedList = numbers.reversed.toList(); // reversedList: [3, 5, 1]

8. Slicing and Modifying Lists

  • sublist(): Returns a new list containing a subset of the elements.

    var subList = numbers.sublist(1, 3); // subList: [5, 6]
  • fillRange(): Replaces a range of elements in the list with a specified value.

    numbers.fillRange(0, 2, 10); // numbers: [10, 10, 6, 3]

Conclusion

Dart's List collection provides a rich set of functions that allow you to create, manipulate, and access ordered collections efficiently. Understanding these common functions helps you work with lists effectively, making it easier to manage data in your applications. Whether you’re adding elements, removing them, or iterating through a list, Dart’s list methods simplify these tasks and enhance your coding experience.