×

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 Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 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 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 Type Test Operators

Type Test Operators in Dart These operators are used to check the types of expressions at runtime. Dart is a typed language, and we often want to assert that a value...

1 minute read.

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 minutes read.

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 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 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.

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 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 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 Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 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 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 Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Dart Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

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 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 Single-Page Application Architecture

In a single page application architecture, the source code for a single web page loads the entire application. The responsibility of building the user interface and requesting data from the...

2 minutes read.

Dart Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.