Begginer's guide to using List in Dart/Flutter

Thu, Dec 1, 2022

Read in 3 minutes

In this post we will take a look at how to use List in Dart/Flutter with its various methods.

In Dart, a list is a collection of objects that are ordered and indexed. A list is similar to an array in other programming languages, but it has additional functionality like dynamically growing or shrinking in size as elements are added or removed. A list can contain elements of any type, including numbers, strings, and even other lists.

Here are some common operations that can be performed on Dart lists, along with examples:

  1. Creating a List To create a list in Dart, you can use the List constructor, which takes an optional length argument, or you can use list literals, which are denoted by square brackets [].

    // Using the List constructor
    List<int> numbers = List(5);
    numbers[0] = 1;
    numbers[1] = 2;
    numbers[2] = 3;
    numbers[3] = 4;
    numbers[4] = 5;
    
    // Using list literals
    List<String> names = ['Alice', 'Bob', 'Charlie'];
    
  2. Accessing Elements in a List Elements in a list can be accessed using their index. The first element in a list has an index of 0, the second element has an index of 1, and so on.

    List<int> numbers = [1, 2, 3, 4, 5];
    
    // Accessing an element by index
    int firstNumber = numbers[0]; // 1
    
    // Changing an element by index
    numbers[3] = 10;
    
    // Getting the length of a list
    int length = numbers.length; // 5
    
  3. Adding and Removing Elements in a List Elements can be added or removed from a list using various methods.

    List<String> names = ['Alice', 'Bob', 'Charlie'];
    
    // Adding a single element to the end of the list
    names.add('David');
    
    // Adding multiple elements to the end of the list
    names.addAll(['Eve', 'Frank']);
    
    // Inserting an element at a specific index
    names.insert(1, 'Grace');
    
    // Removing an element by index
    names.removeAt(2);
    
    // Removing the last element
    names.removeLast();
    
    // Removing a specific element
    names.remove('Bob');
    
    // Removing all elements from the list
    names.clear();
    
  4. Iterating Over a List There are several ways to iterate over the elements in a list.

    List<String> names = ['Alice', 'Bob', 'Charlie'];
    
    // Using a for loop
    for (int i = 0; i < names.length; i++) {
      print(names[i]);
    }
    
    // Using a for-in loop
    for (String name in names) {
      print(name);
    }
    
    // Using a forEach() method
    names.forEach((name) => print(name));
    
  5. Sorting a List A list can be sorted using the sort() method.

    List<int> numbers = [3, 1, 4, 2, 5];
    
    // Sorting the list in ascending order
    numbers.sort();
    
    // Sorting the list in descending order
    numbers.sort((a, b) => b.compareTo(a));
    

Dart lists provide a flexible way to work with collections of data. They can be used to store any type of object, and there are many built-in methods for adding, removing, and iterating over elements in a list.


Shohruh AK



Category:

Flutter

Tags: