×

Dart 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 cannot be changed at runtime.
    • Growable list : The size of this list is dynamic and can be changed at runtime.
  1. Set : It represents a collection of objects in which every object can appear only once.
  2. 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.
  3. 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 :

Program

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
  1. 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 :

Program

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
  1. 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 Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.

Dart Arithmetic Operators

Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc. They are binary operators that work upon two operands. Consider A...

2 minutes 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.

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.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

2 minutes read.

Dart Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not...

2 minutes read.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

3 minutes read.

Dart Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 minutes read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

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.

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.

Dart Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Dart Type inference

The task of interpreting the data types of fields, methods, local variables and most generic type arguments is handled by the static analyser. However, when not enough information is provided...

5 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

1 minute read.