×

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 Sets

Sets data type in Dart A set is an unordered collection of items that are unique. Dart infers that sets tale strings as values. Example, var languages = { ‘Dart’ , ‘Flutter’, ‘Python’,...

3 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 Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart If statement

If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true....

1 minute 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 If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Dart Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

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

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

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.

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 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 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 Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

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

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.

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 Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.