Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

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