Dynamic List in Dart and Flutter

In Dart, we get two types of List. One is of traditional type – fixed length. Another is growable or dynamic List.

Consider a static List or a fixed length List. That is, an array. It is a collection of similar type of data. It could be either integer, double, or string. Moreover, it should have a fixed length while we declare it.

Within that range of length, we can insert data, or modify data at a specific position. Even we can remove a data and make the memory space empty.

Whenever we declare a fixed length List, the memory manager fixes an amount of memory for that list or array.

When we write, ‘int array[4]’, it means, memory manager allocates 4 bytes each for individual address, altogether 16 bytes are allocated.

In language like C or C++, if we want to insert one more data, in whatever position we can imagine; the task becomes tedious. First of all, the memory manager has already fixed a space.

As a result, we need to create a larger array. Copy the whole array to the new memory space and at the same time empty the old space.

Fundamentally, we can state the Data Structure in the following way.


Data Structure at a glance
Data Structure at a glance

To make a list dynamic, we might think an array with maximum size that particular data type allows us to use.

Since the list or array is a contiguous block of memory, a large space remains unused.

Suppose we declare an array of ‘n’ length. We start with 10 data. Next, as per our need we start adding data to the array.

If we add one single data at the very beginning, we need to shift all other data by one space.

If the data type is integer, we need not add 4 bytes more at the end in this case particularly because we have kept that space beforehand, after that we need to shift the other data accordingly.

Whatever we do, a large space still remains unused. In terms of memory allocation, this process does not seem friendly.

Since the length of an array is fixed, it works on a constant time.

Now, new languages come up to solve the limitations of older language. Consider the Dart. In Dart, we get two types of List. One is of traditional type – fixed length. Another is growable or dynamic List.

Enough talking, let us see the first code – fixed length List.

   /*
    The list is a simple ordered group of objects. Creating
    a List seems easy because Dart core libraries have the necessary support
    and a List class. There are two types of Lists.
    */
    void main(){
    listFunction();
    }

    int listFunction(){
    List<int> nameOfTest = List(3);
    nameOfTest[0] = 1;
    nameOfTest[1] = 2;
    nameOfTest[2] = 3;
    //there are three methods to capture the list
    //1. method
    for(int element in nameOfTest){
        print(element);
    }
    print("-----------");
    //2. method
    nameOfTest.forEach((v) => print('${v}'));
    print("-----------");
    //3. method
    for(int i = 0; i < nameOfTest.length; i++){
        print(nameOfTest[i]);
    }
    }

    // output
    1
    2
    3
    -----------
    1
    2
    3
    -----------
    1
    2
    3

In Dart, everything is object. Therefore, it is a collection of similar objects. Here it is integer. Next, we will make this list dynamic, and see how it works.

    void main(){
    growableList();
    }

    Function growableList(){
    //1. method
    List<String> names = List();
    names.add("Mana");
    names.add("Babu");
    names.add("Gopal");
    names.add("Pota");
    //there are two methods to capture the list
    print("-----------");
    //1. method
    names.forEach((v) => print('${v}'));
    print("-----------");
    //2. method
    for(int i = 0; i < names.length; i++){
        print(names[i]);
    }
    }

// output
    -----------
    Mana
    Babu
    Gopal
    Pota
    -----------
    Mana
    Babu
    Gopal
    Pota

Growable Lists are dynamic in nature. We can dynamically add any number of elements.

In addition, we can also remove it by a simple method: ‘names.remove(“any name”)’.

We can also use the key; as this ordered list starts from 0.

So we can remove the first name just by passing this key value: ‘names.removeAt(0)’.

We use the ‘removeAt(key)’ method for that operation. We can also clear the whole List just by typing: ‘names.clear()’.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

Courses at Educative

GitHub repository

Technical blog

Twitter


Posted

in

, ,

by

Comments

3 responses to “Dynamic List in Dart and Flutter”

  1. […] are going to build a NoWar Flutter App. While we build this app, we will discuss a few important list-concepts in Flutter and Dart. We will use various list methods. And one of them is the list.asMap() […]

  2. […] The same way, we can create a drop down menu item widget using a constant list of items. […]

  3. […] Let’s try to understand this first. Since the List.generate constructor uses length and index property, we can use them to build our list. […]

Leave a Reply