×

Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the Dart library ‘ dart : core ’ and some other classes.

Such dart collections are classified as follows :

1. List : It is an ordered list of objects that is implemented using List class. This class enables the creation and manipulation of lists.

There are following two types of lists :

  • Fixed length list : The size of this list is fixed and can not be changed at runtime.
  • Growable list : The size of this list is dynamic and can be changed at runtime.

2. Set : It represents a collection of objects in which every object can appear only once.

3. Maps : It consists of a key-value pair that can belong to any data type. It is completely dynamic, that means it can grow and shrink at runtime.

4. Queue : It is a collection from where we can add and delete items from both ends.

Here are some of the common collection methods :

1. isEmpty( ) or isNotEmpty( ) :

These two functions are used to check whether a list, set, or map has items or not, that is, whether it is empty or not.

Consider the following example that explains the concept of these functions :

Example – 1 :

void main( )
{ 
  // declaraing a list ' coffees '
  var coffees = [ ] ;
  
  // declaring a list ' teas '
  var teas = [ ' green ', ' black ', ' chamomile ', ' earl grey ' ] ;
  
  // checking whether the list ' coffees ' is empty or not
  print( " Is the list of coffees empty ? : " ) ;
  print( coffees.isEmpty ) ;
  
  // checking whether the list ' teas ' is empty or not
  print( " Is the list of teas empty ? : " ) ;
  print( teas.isEmpty ) ;
}

Output :

Is the list of coffees empty ? : 
    true
    Is the list of teas empty ? : 
    false

2. forEach( ) :

This function is used to apply a certain function or condition to each item in a list, set, or map.

Consider the following example that explains the concept of this  function :

Example – 2 :

void main( )
{   
    // defining the list ' teas '
    var teas = [ ' green ', ' black ', ' chamomile ', ' earl grey ' ] ;  
    
    // converting the each item of the list in the upper case
    var t = teas.map( ( tea ) => tea.toUpperCase( ) ) ;
  
    // printing each item of the list
    t.forEach( print ) ;
}

Output :

GREEN 
BLACK 
CHAMOMILE 
EARL GREY

3. where( ) :

This function is used to retrieve all the items that match to a particular condition listen in the where( ) function. Similarly, the functions any( ) and every( ) can be used to check whether some or each item in the list matches a condition.

Consider the following example that explains the concept of this function :

Example – 3 :

void main( )
{
   var colours = [ ' green ', ' black ', ' red ', ' white ' ] ;
  
  // black is the darkest colour of all listed colors
  bool isdark( String colName ) => colName == ' black ' ;
  
  // Use where( ) function to retrive the item that returns true
  // from the isdark( ) function. 
  // Here, it returns the colour that is dark in the list
  print( colours.where( isdark ) ) ;
  
  // Use any( ) function to check whether any of the item in the
  // collection returns satisfies the isdark( ) function
  // Here, it returns true if any of the colour is dark in the list
  print( colours.any( isdark ) ) ;
  
  // Use every( ) function to check whether all the items in a
  // collection satisfies the isdark( ) function
  // Here, it returns true if every colour is dark in the list
  print( colours.every( isdark ) ) ;
}

Output :

( black )
true
false

Related Topics

Dart Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart Basics

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute read.

Dart Packages

Every programming language has some in-built functions stored inside the header files that makes the task much easier for the programmer. The Dart Packages refer to the compilation of a...

4 minutes read.

Dart Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 minutes read.

Rust vs Dart

Rust is a system-level programming language that stands next to C ++ in terms of syntax, but offers greater speed and memory security. Dart, on the other hand, is an...

2 minutes read.

Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes,...

4 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes read.

Dart Enumerations

Enumeration data type in Dart In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.  Implementation =  enum enum_name {    ...

2 minutes read.

Dart HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.

Dart Operators Precedence and Associativity

Precedence of operators determines the order in which the operators are evaluated if they are grouped together in a sentence. If two operators share an operand then the one with...

5 minutes read.

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 minutes read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

4 minutes read.