Exploring Dart Collection Types : Part 1 — Lists

Collections in any programming language are fundamentals.It allows us to store, retrieve data in the form of a collection per say. Just like in any other programming lanaguage, Dart provides us with built-in Collection types including Lists, Set, Map and Queue and more.

All the collection types are available with dart:collection package. One thing that is common between these collection types is that all of them implements an Iterable. An Iterable is just another object capable of returning its element in iterations or in easy words, one-by-one.

We’ll go through each of the Collection one by one. Let’s start off with the probably the most common amongst all, The List Collection Type.

List:

List, also known as an Array in some languages is simply is an ordered collection of objects which has a length. The list can be iterated through indices, starting off with the zero index and ending at length-1. To define a list in Dart, we enclose its elements with square brackets []. Let’s see that in code:

If we want our List to be of specific types, we can restrict the element types using the angle brackets<> as such:

Now let’s get to the interesting part, We can not only create lists and iterate through them to get the element we want but we can also perform operations on it. Dart Lists offers a bunch of functions. Let’s go through them one by one:

reversed():

Let’s start things off with the reverse() method. I remember, when I started learning Programming with an OG language like C, how complex it was just to reverse a list or an Array as I should say. Dart makes the process so much easier with its reverse() method. You just call it on a List and it returns a reversed version back to you. That simple. Let’s have a look at the code:

shuffle():

The shuffle method like the name tells, shuffles a given list. The method takes an optional parameter where you can define how you want to shuffle the list. Bear in mind, since the Shuffle method does not returns anything, It manipulates the original List on which it is being called on. Let’s see the shuffle() method in action:

subList():

The subList() method returns a new list SubList from the List of elements on which it is called on. The subList() method takes in two parameters: start and end. The sublist contains elements starting from start (inclusive) and ends at ending index with that index being exclusive. Let’s dive into the code to see its magic:

asMap():

asMap() returns the Map(A key-value pair representation of data) - representation of the given String. In that Map, the indices serve as the keys and the values are the corresponding elements of the List. We can further call the getter methods on asMap().keys if we just want to get the keys or asMap().values if we want to only get the values. Let’s have a look at the method in code:

fold():

The return value of fold() function is a single value. The value is determined by the function which is being performed with the fold() function. For instance, It can be used to get a sum of elements in a list as:

Fold takes two parameters, the initialValue and a function. In this example, we iterate through the list with 5 being the initialValue and add up list elements into the initialValue so that:
- In the first iteration, the element at Index 0 is 2, so 5+2 = 7 which now the new value of i.
- In the second iteration i+j, 7+4 = 11 =i;
- In the third of i+j, 11+6= 17 and there is no more element left in the list so the function evaluatation completes and it returns 17.

isEmpty & isNotEmpty():

isEmpty() and isNotEmpty() are two very important functions on Dart Lists which as their name suggests checks if a List is Empty or if a List is not empty respectively.
- isEmpty(): It returns true is the list is empty and false otherwise.
- isNotEmpty(): In case of isNotEmpty(), the situation is other way round, where is returns true in case of list having some elements and false otherwise.

add():

add() is the method that is used to add elements into the list. It takes in the element as a parameter and adds it up into the list on which it is called on as:

reduce():

reduce() method works pretty much the same as the fold method. The only difference between the two is that fold() method has an initial value while the reduce method does not. Let’s see that in code:

any():

any() is the method that takes in a function as a parameter and based on that function returns a boolean in the form or a true or a false. Let’s suppose we want to check if the list contain any negative number. We can use any() in that case as:

every():

every() works pretty much the same as any. The only difference is that any evaulates to a boolean value if even a element satisfies the condition but in case of every(), every element in the list has to satisfy the condition. Let’s see that in code:

getRange():

As the name suggests, the getRange method returns elements from the list which lie in the specific range. The range function takes in two parameters: start and end where start is inclusive and end is exclusive. Since the range function returns an iterable, we call toList() to typeCast the Iterable to a List as follows:

replaceRange():

Need help updating an element within a specific range? The replaceRange() method got you covered. The method takes in 3 parameters, 2 of which are just like getRange() where its the start and end point of range while the third being the value that is to be replaced in the place of the desired element.The element that lies between the two ranges gets replaced. If there is no element withtin the two ranges, the element at start gets replaced. Let’s see that in an example as:

firstWhere():

firstWhere() is the method that takes in a function as a paramater and returns the first element where the condition is satisfied as:

first() & last():

Like the name speaks for itself, the first() method is used to get the first element from the list and the last() method returns the last element from the list. Let’s see that in code:

That’s all for now Folks! Thanks for reading this article ❤️ Will soon be posting the next parts. Stay tuned!

Clap 👏 If this article helped you.

Feel free to post any queries or corrections you think are required ✔
Do leave a feedback so I can improve on my content. Thankyou! 😃

If you’re interested, here are some of my other articles:

I try to teach what I learn. Flutter Developer | Dart Enthusiast