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 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’, ‘Java’ };

There are some salient functions that can be used along with sets,

  1. add( ) or addAll( ) methods are the functions used to add elements in the existing set.
  2. .length is used to extract the number of items in the set.

Program:

void main( )
{
    // declares and defines set languages
    var languages = { 'Dart' , 'Flutter' , 'Python' , 'Java' };
    // prints the values of set
    print( languages );
    
    // creates an empty string
    var lang = < String > {  };
    print( lang );
    // add( ) to add the value in set languages
    languages.add( 'php' );
    print( languages );
  }

Output :

{Dart, Flutter, Python, Java}
{}
{Dart, Flutter, Python, Java, php}

Set Operations in Dart

  1. Union = This operation merges two strings and returns the merged string.
  2. Intersection = This operation returns the common strings out of the two strings.
  3. Difference ( string A, string B ) = This operation returns the string present in string A but not in B.

Consider the following code in Dart that illustrates the above operations:

Program

// Set Operations in Dart
 
void main( )
{
  // declaring set 1 with string values
  var set1 = < String > { "India", "will", "win", "T-20" };
   
  // printing the values of set 1
  print( " Set 1 is as follows : " );
  print( set1 );
   
   
  // declaring set 2 with string values
  var set2 = < String > { "World", "Cup", "this", "year." };
  
  // declaring set 3 with string values
  var set3 = < String > { "World", "India", "win", "year." };
   
  // printing the values of set 2
  print( "\n\n Set 2 is as follows : " );
  print( set2 );
   
   
  // Performing union on set1 and set2
  print( "\n Union of two sets is ${ set1.union( set2 ) } \n " ) ;
   
  // Performing intersection on set1 and set2
  print( " Intersection of sets 1 and 2 is  ${ set1.intersection( set2 ) } \n ");
  
  // Performing intersection on set2 and set3
  print( " Intersection of sets 2 and 3 is ${ set2.intersection( set3 ) } \n " );
   
  // Performing difference ( Set2 - Set1 )
  print( " Difference of sets 2 and 1 is ${ set2.difference( set1 ) } \n " );
  
  // Performing difference ( Set1 - Set2 )
  print( " Difference of sets 1 and 2 is ${ set1.difference( set2 ) } \n " );
  
  // Performing difference ( Set2 - Set3 )
  print( " Difference of sets 2 and 3 is ${ set2.difference( set3 ) } \n " );
     
}

Output

Set 1 is as follows : 
{India, will, win, T-20}


 Set 2 is as follows : 
{World, Cup, this, year.}


 Union of two sets is {India, will, win, T-20, World, Cup, this, year.} 
 Intersection of sets 1 and 2 is  {} 
 Intersection of sets 2 and 3 is {World, year.} 
 Difference of sets 2 and 1 is {World, Cup, this, year.} 
 Difference of sets 1 and 2 is {India, will, win, T-20} 
 Difference of sets 2 and 3 is {Cup, this}

Sets Functions

S. No.SyntaxDescription
1.variable_name.elementAt( index );Returns the element present at the particular index, passed as an argument.
2.variable_name.length;Calculates the length of the set, and returns the same.
3.variable_name.contains( element_name );Checks if the element_name is present in the set or not, and returns true or false accordingly.
4.variable_name.remove( element_name );Deletes the element that is passed as an argument to the function.
5.variable_name.forEach(…);Implements the command defined inside forEach( ) function for all the elements inside the set.
6.variable_name.clear( );Deletes all the element inside the set.