Table of contents
1.
Introduction
2.
Dart Collection Methods
2.1.
sublist()
2.2.
shuffle()
2.3.
reversed
2.4.
asMap()
2.5.
forEach()
2.6.
isEmpty
2.7.
isNotEmpty
3.
Frequently Asked Questions
3.1.
What if you only give one attribute to the sublist method?
3.2.
Does the isEmpty method work for lists only?
3.3.
What is the key of elements generated by the asMap() method?
4.
Conclusion 
Last Updated: Mar 27, 2024

Dart Collection Methods

Author Pradeep Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will look into the Dart Collection Methods. We will discuss some of the common collection methods used to manipulate the elements of a list, string, or map. If you are new to Dart and are yet to set up your environment for the language, you can check out this article.

Dart Collection Methods

Before directly jumping on Collection methods, we suggest you take a quick overview of Dart Collections. Following are some of the common collection methods used to manipulate lists, strings, and maps:

sublist()

This method is used to slice a list. You can either specify the starting and ending index to get all the elements in that range. It will not take the element represented by the ending index. Only the elements in the range [starting_index, ending_index) will be returned. For example,

void main() {
  var l = [2, 4, 6, 8, 10];
  var sublist =l.sublist(1, 4); // all the elements having index in the range [1,4)
  var sublist2 = l.sublist(2); // all the elements after the index 2
  print(sublist);
  print(sublist2);
}

Output:

[4, 6, 8]
[6, 8, 10]

shuffle()

This method is used to shuffle the elements of a list. For example,

void main() {
  var l = [2, 4, 6, 8, 10];
  l.shuffle(); // shuffling the elements of the list
  print(l);
}

Output:

[2, 10, 8, 4, 6]

reversed

To reverse a list, this method is used. For example,

void main() {
  var l = [2, 4, 6, 8, 10];
  // reversing the list
  var reversed_list = l.reversed.toList();
  print(reversed_list);
}

Output:

[10, 8, 6, 4, 2]

asMap()

This method is used to convert a list into map. For example,

void main() {
  var colors = ["red", "blue", "green"];
  // getting map of the colors
  var map_colors = colors.asMap();
  // the key here would be the index of the element and the value would be the element itself.
  print(map_colors);
}

Output:

{0: red, 1: blue, 2: green}

forEach()

You can use forEach() to apply a function to each element of a list, set, or map. For example,

void main() {
  var l = [1, 2, 3, 4];
  print("Following are the list items:");
  // printing all the items of the list
  l.forEach(print);
}

Output:

Following are the list items:
1
2
3
4

isEmpty

We utilise this method to check if a list is empty. It returns true if the list is empty.

// This program demonstrates the working of isEmpty method
void main() {
  var l = [1, 2, 3, 4];
  print("Is l empty?: " + l.isEmpty.toString());
}

Output:

Is l empty?: false

isNotEmpty

This method determines whether or not a list is empty. If the list contains some element, it returns true.

// This program demonstrates the working of isNotEmpty method
void main() {
  var l = [1, 2, 3, 4];
  print("Is l not empty?: " + l.isNotEmpty.toString());
}

Output:

Is l not empty?: true

Frequently Asked Questions

What if you only give one attribute to the sublist method?

If you give only one index to the sublist method, it will return a list of elements starting from that index to the end of the list.

 

Does the isEmpty method work for lists only?

isEmpty method is supported by various collections such as maps, sets, lists, etc.

 

What is the key of elements generated by the asMap() method?

The key of the element is the index of that element in the list, and its value is the value of the element as given in the list.

Conclusion 

In this article, we have extensively discussed some common Collection methods in Dart. We hope that this blog has helped you enhance your knowledge regarding Dart Collection methods, and to learn more, check out our other article on Dart Lists. And also check out the awesome content on the Coding Ninjas Website,
Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass