Using the Lists in Dart in Ubuntu 20.04
To make the usage of the lists in Dart easier for you to understand, we have implemented a few examples, which are explained below:
Example # 1: How to Create a Fixed Length List in Dart?
The lists in the Dart programming language can either have a fixed length, or their length can grow dynamically. The following Dart script will teach you the method of creating a Dart list having a fixed length:
In this example, we have created a list named “myList” by making use of the “new List.filled(5,0)” notation. Here, “5” refers to the length of the list, whereas “0” refers to the element with which this list will be initialized before assigning any other values to its indexes. You can change the length of this list according to your choice. After that, we simply assigned the elements to all the indexes of this list. You need to remember that the indexing of the lists in Dart always begins with “0”. Then, we have just used the list’s name inside the “print” statement to display all the elements of this list on the terminal.
To execute this Dart script, we have used the command shown below:
The elements of our fixed length Dart list are shown in the following image:
Example # 2: How to Create a Grow-able List in Dart?
In this example, we will share with you the two methods of creating a grow-able list in Dart, which are as follows:
Method # 1: Grow-able List With Single-Step Initialization
You can create a grow-able list with single-step initialization in Dart by making use of the script shown below:
In this script, we have simply declared a list by assigning values to its indexes at once, and then we have just printed these values on the terminal. You can also add more elements to this list since it does not have a pre-defined fixed length.
The elements of this grow-able Dart list are shown in the following image:
Method # 2: Grow-able List With Multi-Step Initialization
The other method of creating a grow-able list in Dart makes use of multi-step initialization, which is shown in the Dart script below:
In this example, we have created a grow-able Dart list with the statement “var myList = [].” After that, we added as many elements to this list as we wanted one by one. Again, we have used a “print” statement for printing these elements on the terminal.
The elements of this grow-able Dart list are shown in the following image:
Example # 3: How to Add Multiple Elements at Once to a Grow-able List in Dart?
You can also add multiple elements to a grow-able Dart list at once by using the Dart script shown below:
In this example, we have created a grow-able Dart list in the same manner as explained in the second method of our second example. However, we have added multiple elements to this list at once by using the “addAll” function. Afterward, we printed the updated list on the terminal.
The Dart list before and after adding multiple elements to it at once is shown in the following image:
Example # 4: How to Insert an Element at a Specified Index of a Grow-able List in Dart?
You can also insert an element of your choice at a specific index of a grow-able Dart list by using the Dart script shown below:
In this example, we wanted to insert the element “0” at the “0th” index of an already created grow-able Dart list. For doing that, we have used the “myList.insert(0,0)” notation where the first “0” corresponds to the index where we need to do the insertion, whereas the second “0” corresponds to the element to be inserted. Then, we printed this updated list on the terminal.
The elements of our list after insertion are shown in the following image:
Example # 5: How to Remove an Element From a List in Dart?
For removing an element from a Dart list, you can take help from the Dart script shown below:
In this example, we wanted to remove the element “2” from our list because of which we have used the “myList.remove(2)” statement. To check if the specified element has been removed successfully or not, we have displayed the updated list on the terminal.
You can see from the following output that our specified element has been removed successfully from our list:
Example # 6: How to Remove an Element at a Specific Index of a List in Dart?
You can also remove an element from a Dart list by specifying its index, as shown in the Script below:
In this example, we wished to remove the element present on our list’s “2nd” index, technically the “3rd” element of the list. We have just used the “myList.removeAt(2)” notation.
The output shown in the following image depicts that the element at the specified index has been removed successfully from our Dart list.
Conclusion
Today, we wanted to share how the lists can be used in the Dart programming language in Ubuntu 20.04. In this regard, we shared different simple yet usable examples with you while answering different questions. In this way, you will not only be able to learn the usage of the lists in Dart but will also be able to gain a deep insight into the different functions that are associated with this data structure.