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 :
- 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.
- Set : It represents a collection of objects in which every object can appear only once.
- 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.
- Queue : It is a collection from where we can add and delete items from both ends.
Here are some of the common collection methods :
- 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
- 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
- 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