Useful List methods in Dart

Darshan Kawar
Oct 26, 2019 · 6 min read

List is one of four types of collection Dart offers. It is equivalent to Array and is an ordered collection of items, starting with index 0.

In this article, we’ll take a look at various List methods that may not be used frequently but are very useful in retrieving data for unique cases, with example of each.


sublist():

This method returns a new list containing elements from index between start and end. Note that end element is exclusive while start is inclusive.

 var myList= [1,2,3,4,5];
print(myList.sublist(1,3)); // [2,3]

If end parameter is not provided, it returns all elements starting from start till length of the list.

print(myList.sublist(1)); // [2,3,4,5]

shuffle():

This method re-arranges order of the elements in the given list randomly.

 myList.shuffle();
print('$myList'); // [5,4,3,1,2]

reversed:

reversed is a getter which reverses iteration of the list depending upon given list order.

 var descList= [6,5,4,3,2,1];
print(descList.reversed.toList()); // [1,2,3,4,5,6]

var ascList = [1,2,3,4,5,6];
print(ascList.reversed.toList()); // [6,5,4,3,2,1]

asMap():

This method returns a map representation of the given list. The key would be the indices and value would be the corresponding elements of the list.

 List<String> sports = ['cricket', 'football', 'tennis', 'baseball'];
Map<int, String> map = sports.asMap();
print(map); // {0: cricket, 1: football, 2: tennis, 3: baseball}

whereType():

This method returns iterable with all elements of specific data type.

Let’s say we have a list with mix data such as String and int and we just want to retrieve int data from it, then we would use whereType method as:

 var mixList = [1, "a", 2, "b", 3, "c", 4, "d"];
var num = mixList.whereType<int>();
print(num); // (1, 2, 3, 4)

Similarly, we can retrieve only String data by specifying mixList.whereType<String>();

getRange():

This method returns elements from specified range [start] to [end] in same order as in the given list. Note that, start element is inclusive but end element is exclusive.

 var myList = [1, 2, 3, 4, 5];
print(myList.getRange(1,4)); // (2, 3, 4)

replaceRange():

This method helps to replace / update some elements of the given list with the new ones. The start and end range need to be provided alongwith the value to be updated in that range.

 var rList= [0,1,2,3,4,5,6];
rList.replaceRange(2,3, [10]);
print('$rList'); // [0, 1, 10, 3, 4, 5, 6]

firstWhere():

This method returns the first element from the list when the given condition is satisfied.

var firstList = [1,2,3,4,5,6];
print(firstList.firstWhere((i) => i < 4)); // 1

var sList = ['one', 'two', 'three', 'four'];
print(sList.firstWhere((i) => i.length > 3)); // three

Similarly, lastWhere() returns last element from the list when given condition is met. where() returns new list that matches the condition.

singleWhere():

This method returns the first matching element from the list when there’s an exact match.

 var mList = [1, 2, 3, 4];
print(mList.singleWhere((i) => i == 3)); // 3

If the given list contains a duplicate, then singleWhere method retuns an exception. In that case, we can use firstWhere method which returns the first matching element irrespective of repeating / duplicates in the list.

 var sList = [1, 2, 3, 3, 4];
print(sList.singleWhere((i) => i == 3));
// Bad state: Too many elements

print(sList.firstWhere((i) => i == 3)); // 3

fold():

This method returns a single value by iterating all elements of given list along with an initialValue , ie, if we want sum of all elements or product of all elements, then, fold helps us to do that.

 var lst = [1,2,3,4,5];
var res = lst.fold(5, (i, j) => i + j);
print('res is ${res}'); // res is 20

In above example, 5 is the initialValue and then fold starts the iteration from index 0, adds element to the initialValue (5 + 1) and moves ahead till index 4 like (6+2, 8+3, 11+4, 15+5). Hence, the single value, ie sum of all elements + intialValue is returned as 20.

reduce():

This method is very similar to fold and returns a single value by iterating all elements of given list. Only difference is, this method doesn’t take any initialValue and the given list should be non-empty.

 var lst = [1,2,3,4,5];
var res = lst.reduce((i, j) => i + j);
print('res is ${res}'); // res is 15

followedBy():

This method appends new iterables to the given list.

var sportsList = ['cricket', 'tennis', 'football'];
print(sportsList.followedBy(['golf', 'chess']).toList());
// [cricket, tennis, football, golf, chess]

any():

This method returns a boolean depending upon whether any element satisfies the condition or not.

print(sportsList.any((e) => e.contains('cricket'))); // true

every():

This method returns a boolean depending upon whether all elements satisfies the condition or not.

print(sportsList.every((e) => e.startsWith('a'))); // false

take():

This method returns iterable starting from index 0 till the count provided from given list.

print(sportsList.take(2));  // (cricket, tennis)

skip():

This method ignores the elements starting from index 0 till count and returns remaining iterable from given list.

print(sportsList.skip(2));  // (football)

There are also other methods and properties that List provides. Here’s a rundown of each:

add: adds new element in given list.

length: returns total number of elements in given list.

isEmpty: returns boolean if given list is empty or not.

isNotEmpty: checks and returns boolean if given list has elements.

first: returns first element from given list.

last: returns last element from given list.

clear: clears the given list.


That’s all I have in this article. Thanks for reading through and I hope you liked what you read here. Feel free to comment below your thoughts or any comment /suggestions/feedback on this article or you may also connect with me on Twitter, LinkedIn and Github.

My other articles on Flutter are:

Flutter Community

Articles and Stories from the Flutter Community

Darshan Kawar

Written by

Remote Software Engineer (Flutter) @QuickCarlapp. Android Developer Nanodegree Certified. Previously, Android Automation Test Engineer.

Flutter Community

Articles and Stories from the Flutter Community

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade