Category: Dart

  • Collection

    Dart, unlike other programming languages, doesn’t support arrays. Dart collections can be used to replicate data structures like an array. The dart:core library and other classes enable Collection support in Dart scripts.

    Dart collections can be basically classified as −

    Sr.NoDart collection & Description
    1ListA List is simply an ordered group of objects. The dart:core library provides the List class that enables creation and manipulation of lists.Fixed Length List − The list’s length cannot change at run-time.Growable List − The list’s length can change at run-time.
    2SetSet represents a collection of objects in which each object can occur only once. The dart:core library provides the Set class to implement the same.
    3MapsThe Map object is a simple key/value pair. Keys and values in a map may be of any type. A Map is a dynamic collection. In other words, Maps can grow and shrink at runtime. The Map class in the dart:core library provides support for the same.
    4QueueA Queue is a collection that can be manipulated at both ends. Queues are useful when you want to build a first-in, first-out collection. Simply put, a queue inserts data from one end and deletes from another end. The values are removed / read in the order of their insertion.

    Iterating Collections

    The Iterator class from the dart:core library enables easy collection traversal. Every collection has an iterator property. This property returns an iterator that points to the objects in the collection.

    Example

    The following example illustrates traversing a collection using an iterator object.

    import 'dart:collection'; 
    void main() { 
       Queue numQ = new Queue(); 
       numQ.addAll([100,200,300]);  
       Iterator i= numQ.iterator; 
       
       while(i.moveNext()) { 
    
      print(i.current); 
    } }

    The moveNext() function returns a Boolean value indicating whether there is a subsequent entry. The current property of the iterator object returns the value of the object that the iterator currently points to.

    This program should produce the following output −

    100 
    200 
    300
    
  • Object

    Object-Oriented Programming defines an object as “any entity that has a defined boundary.” An object has the following −

    • State − Describes the object. The fields of a class represent the object’s state.
    • Behavior − Describes what an object can do.
    • Identity − A unique value that distinguishes an object from a set of similar other objects. Two or more objects can share the state and behavior but not the identity.

    The period operator (.) is used in conjunction with the object to access a class’ data members.

    Example

    Dart represents data in the form of objects. Every class in Dart extends the Object class. Given below is a simple example of creating and using an object.

    class Student { 
       void test_method() { 
    
      print("This is a  test method"); 
    } void test_method1() {
      print("This is a  test method1"); 
    } } void main() { Student s1 = new Student(); s1.test_method(); s1.test_method1(); }

    It should produce the following output −

    This is a test method 
    This is a test method1
    

    The Cascade operator (..)

    The above example invokes the methods in the class. However, every time a function is called, a reference to the object is required. The cascade operator can be used as a shorthand in cases where there is a sequence of invocations.

    The cascade ( .. ) operator can be used to issue a sequence of calls via an object. The above example can be rewritten in the following manner.

    class Student { 
       void test_method() { 
    
      print("This is a  test method"); 
    } void test_method1() {
      print("This is a  test method1"); 
    } } void main() { new Student() ..test_method() ..test_method1(); }

    It should produce the following output −

    This is a test method 
    This is a test method1
    

    The toString() method

    This function returns a string representation of an object. Take a look at the following example to understand how to use the toString method.

    void main() { 
       int n = 12; 
       print(n.toString()); 
    } 

    It should produce the following output −

    12
    
  • Classes

    Dart is an object-oriented language. It supports object-oriented programming features like classes, interfaces, etc. A class in terms of OOP is a blueprint for creating objects. A class encapsulates data for the object. Dart gives built-in support for this concept called class.

    Declaring a Class

    Use the class keyword to declare a class in Dart. A class definition starts with the keyword class followed by the class name; and the class body enclosed by a pair of curly braces. The syntax for the same is given below −

    Syntax

    class class_name {  
       <fields> 
       <getters/setters> 
       <constructors> 
       <functions> 
    }
    

    The class keyword is followed by the class name. The rules for identifiers must be considered while naming a class.

    A class definition can include the following −

    • Fields − A field is any variable declared in a class. Fields represent data pertaining to objects.
    • Setters and Getters − Allows the program to initialize and retrieve the values of the fields of a class. A default getter/ setter is associated with every class. However, the default ones can be overridden by explicitly defining a setter/ getter.
    • Constructors − responsible for allocating memory for the objects of the class.
    • Functions − Functions represent actions an object can take. They are also at times referred to as methods.

    These components put together are termed as the data members of the class.

    Example: Declaring a class

    class Car {  
       // field 
       String engine = "E1001";  
       
       // function 
       void disp() { 
    
      print(engine); 
    } }

    The example declares a class Car. The class has a field named engine. The disp() is a simple function that prints the value of the field engine.

    Creating Instance of the class

    To create an instance of the class, use the new keyword followed by the class name. The syntax for the same is given below −

    Syntax

    var object_name = new class_name([ arguments ])
    
    • The new keyword is responsible for instantiation.
    • The right-hand side of the expression invokes the constructor. The constructor should be passed values if it is parameterized.

    Example: Instantiating a class

    var obj = new Car("Engine 1")

    Accessing Attributes and Functions

    A class’s attributes and functions can be accessed through the object. Use the ‘.’ dot notation (called as the period) to access the data members of a class.

    //accessing an attribute 
    obj.field_name  
    
    //accessing a function 
    obj.function_name()

    Example

    Take a look at the following example to understand how to access attributes and functions in Dart −

    void main() { 
       Car c= new Car(); 
       c.disp(); 
    }  
    class Car {  
       // field 
       String engine = "E1001";  
       
       // function 
       void disp() { 
    
      print(engine); 
    } }

    The output of the above code is as follows −

    E1001
    

    Dart Constructors

    A constructor is a special function of the class that is responsible for initializing the variables of the class. Dart defines a constructor with the same name as that of the class. A constructor is a function and hence can be parameterized. However, unlike a function, constructors cannot have a return type. If you don’t declare a constructor, a default no-argument constructor is provided for you.

    Syntax

    Class_name(parameter_list) { 
       //constructor body 
    }
    

    Example

    The following example shows how to use constructors in Dart −

    void main() { 
       Car c = new Car('E1001'); 
    } 
    class Car { 
       Car(String engine) { 
    
      print(engine); 
    } }

    It should produce the following output −

    E1001 
    

    Named Constructors

    Dart provides named constructors to enable a class define multiple constructors. The syntax of named constructors is as given below −

    Syntax : Defining the constructor

    Class_name.constructor_name(param_list)
    

    Example

    The following example shows how you can use named constructors in Dart −

    void main() {           
       Car c1 = new Car.namedConst('E1001');                                       
       Car c2 = new Car(); 
    }           
    class Car {                   
       Car() {                           
    
      print("Non-parameterized constructor invoked");
    } Car.namedConst(String engine) {
      print("The engine is : ${engine}");    
    } }

    It should produce the following output −

    The engine is : E1001 
    Non-parameterized constructor invoked
    

    The this Keyword

    The this keyword refers to the current instance of the class. Here, the parameter name and the name of the class’s field are the same. Hence to avoid ambiguity, the class’s field is prefixed with the this keyword. The following example explains the same −

    Example

    The following example explains how to use the this keyword in Dart −

    void main() { 
       Car c1 = new Car('E1001'); 
    }  
    class Car { 
       String engine; 
       Car(String engine) { 
    
      this.engine = engine; 
      print("The engine is : ${engine}"); 
    } }

    It should produce the following output −

    The engine is : E1001
    

    Dart Class ─ Getters and Setters

    Getters and Setters, also called as accessors and mutators, allow the program to initialize and retrieve the values of class fields respectively. Getters or accessors are defined using the get keyword. Setters or mutators are defined using the set keyword.

    A default getter/setter is associated with every class. However, the default ones can be overridden by explicitly defining a setter/ getter. A getter has no parameters and returns a value, and the setter has one parameter and does not return a value.

    Syntax: Defining a getter

    Return_type  get identifier 
    { 
    } 
    

    Syntax: Defining a setter

    set identifier 
    { 
    }
    

    Example

    The following example shows how you can use getters and setters in a Dart class −

    class Student { 
       String name; 
       int age; 
    
    String get stud_name {
      return name; 
    }
    void set stud_name(String name) {
      this.name = name; 
    } void set stud_age(int age) {
      if(age&lt;= 0) { 
        print("Age should be greater than 5"); 
      }  else { 
         this.age = age; 
      } 
    } int get stud_age {
      return age;     
    } } void main() { Student s1 = new Student(); s1.stud_name = 'MARK'; s1.stud_age = 0; print(s1.stud_name); print(s1.stud_age); }

    This program code should produce the following output −

    Age should be greater than 5 
    MARK 
    Null 
    

    Class Inheritance

    Dart supports the concept of Inheritance which is the ability of a program to create new classes from an existing class. The class that is extended to create newer classes is called the parent class/super class. The newly created classes are called the child/sub classes.

    A class inherits from another class using the ‘extends’ keyword. Child classes inherit all properties and methods except constructors from the parent class.

    Syntax

    class child_class_name extends parent_class_name 
    

    Note − Dart doesn’t support multiple inheritance.

    Example: Class Inheritance

    In the following example, we are declaring a class Shape. The class is extended by the Circle class. Since there is an inheritance relationship between the classes, the child class, i.e., the class Car gets an implicit access to its parent class data member.

    void main() { 
       var obj = new Circle(); 
       obj.cal_area(); 
    }  
    class Shape { 
       void cal_area() { 
    
      print("calling calc area defined in the Shape class"); 
    } } class Circle extends Shape {}

    It should produce the following output −

    calling calc area defined in the Shape class
    

    Types of Inheritance

    Inheritance can be of the following three types −

    • Single − Every class can at the most extend from one parent class.
    • Multiple − A class can inherit from multiple classes. Dart doesn’t support multiple inheritance.
    • Multi-level − A class can inherit from another child class.

    Example

    The following example shows how multi-level inheritance works −

    void main() { 
       var obj = new Leaf(); 
       obj.str = "hello"; 
       print(obj.str); 
    }  
    class Root { 
       String str; 
    }  
    class Child extends Root {}  
    class Leaf extends Child {}  
    //indirectly inherits from Root by virtue of inheritance

    The class Leaf derives the attributes from Root and Child classes by virtue of multi-level inheritance. Its output is as follows −

    hello
    

    Dart – Class Inheritance and Method Overriding

    Method Overriding is a mechanism by which the child class redefines a method in its parent class. The following example illustrates the same −

    Example

    void main() { 
       Child c = new Child(); 
       c.m1(12); 
    } 
    class Parent { 
       void m1(int a){ print("value of a ${a}");} 
    }  
    class Child extends Parent { 
       @override 
       void m1(int b) { 
    
      print("value of b ${b}"); 
    } }

    It should produce the following output −

    value of b 12
    

    The number and type of the function parameters must match while overriding the method. In case of a mismatch in the number of parameters or their data type, the Dart compiler throws an error. The following illustration explains the same −

    import 'dart:io'; 
    void main() { 
       Child c = new Child(); 
       c.m1(12); 
    } 
    class Parent { 
       void m1(int a){ print("value of a ${a}");} 
    } 
    class Child extends Parent { 
       @override 
       void m1(String b) { 
    
      print("value of b ${b}");
    } }

    It should produce the following output −

    value of b 12
    

    The static Keyword

    The static keyword can be applied to the data members of a class, i.e., fields and methods. A static variable retains its values till the program finishes execution. Static members are referenced by the class name.

    Example

    class StaticMem { 
       static int num;  
       static disp() { 
    
      print("The value of num is ${StaticMem.num}")  ; 
    } } void main() { StaticMem.num = 12; // initialize the static variable } StaticMem.disp(); // invoke the static method }

    It should produce the following output −

    The value of num is 12
    

    The super Keyword

    The super keyword is used to refer to the immediate parent of a class. The keyword can be used to refer to the super class version of a variable, property, or method. The following example illustrates the same −

    Example

    void main() { 
       Child c = new Child(); 
       c.m1(12); 
    } 
    class Parent { 
       String msg = "message variable from the parent class"; 
       void m1(int a){ print("value of a ${a}");} 
    } 
    class Child extends Parent { 
       @override 
       void m1(int b) { 
    
      print("value of b ${b}"); 
      super.m1(13); 
      print("${super.msg}")   ; 
    } }

    It should produce the following output −

    value of b 12 
    value of a 13 
    message variable from the parent class
    
  • Interfaces

    An interface defines the syntax that any entity must adhere to. Interfaces define a set of methods available on an object. Dart does not have a syntax for declaring interfaces. Class declarations are themselves interfaces in Dart.

    Classes should use the implements keyword to be able to use an interface. It is mandatory for the implementing class to provide a concrete implementation of all the functions of the implemented interface. In other words, a class must redefine every function in the interface it wishes to implement.

    Syntax: Implementing an Interface

    class identifier implements interface_name
    

    Example

    In the following program, we are declaring a class Printer. The ConsolePrinter class implements the implicit interface declaration for the Printer class. The main function creates an object of the ConsolePrinter class using the new keyword. This object is used to invoke the function print_data defined in the ConsolePrinter class.

    void main() { 
       ConsolePrinter cp= new ConsolePrinter(); 
       cp.print_data(); 
    }  
    class Printer { 
       void print_data() { 
    
      print("__________Printing Data__________"); 
    } } class ConsolePrinter implements Printer { void print_data() {
      print("__________Printing to Console__________"); 
    } }

    It should produce the following output −

    __________Printing to Console__________
    

    Implementing Multiple Interfaces

    A class can implement multiple interfaces. The interfaces are separated by a comma. The syntax for the same is given below −

    class identifier implements interface-1,interface_2,interface_4…….
    

    The following example shows how you can implement multiple interfaces in Dart −

    void main() { 
       Calculator c = new Calculator(); 
       print("The gross total : ${c.ret_tot()}"); 
       print("Discount :${c.ret_dis()}"); 
    }  
    class Calculate_Total { 
       int ret_tot() {} 
    }  
    class Calculate_Discount { 
       int ret_dis() {} 
    }
    class Calculator  implements Calculate_Total,Calculate_Discount { 
       int ret_tot() { 
    
      return 1000; 
    } int ret_dis() {
      return 50; 
    } }

    It should produce the following output −

    The gross total: 1000 
    Discount:50 
    
  • Functions

    Functions are the building blocks of readable, maintainable, and reusable code. A function is a set of statements to perform a specific task. Functions organize the program into logical blocks of code. Once defined, functions may be called to access code. This makes the code reusable. Moreover, functions make it easy to read and maintain the program’s code.

    A function declaration tells the compiler about a function’s name, return type, and parameters. A function definition provides the actual body of the function.

    Sr.NoFunctions & Description
    1Defining a FunctionA function definition specifies what and how a specific task would be done.
    2Calling a FunctionA function must be called so as to execute it.
    3Returning FunctionsFunctions may also return value along with control, back to the caller.
    4Parameterized FunctionParameters are a mechanism to pass values to functions.

    Optional Parameters

    Optional parameters can be used when arguments need not be compulsorily passed for a function’s execution. A parameter can be marked optional by appending a question mark to its name. The optional parameter should be set as the last argument in a function.

    We have three types of optional parameters in Dart −

    Sr.NoParameter & Description
    1Optional Positional ParameterTo specify optional positional parameters, use square [] brackets.
    2Optional named parameterUnlike positional parameters, the parameter’s name must be specified while the value is being passed. Curly brace {} can be used to specify optional named parameters.
    3Optional Parameters with Default ValuesFunction parameters can also be assigned values by default. However, such parameters can also be explicitly passed values.

    Recursive Dart Functions

    Recursion is a technique for iterating over an operation by having a function call to itself repeatedly until it arrives at a result. Recursion is best applied when you need to call the same function repeatedly with different parameters from within a loop.

    Example

    void main() { 
       print(factorial(6));
    }  
    factorial(number) { 
       if (number <= 0) {         
    
      // termination case 
      return 1; 
    } else {
      return (number * factorial(number - 1));    
      // function invokes itself 
    } }

    It should produce the following output −

    720
    

    Lambda Functions

    Lambda functions are a concise mechanism to represent functions. These functions are also called as Arrow functions.

    Syntax

    [return_type]function_name(parameters)=>expression;
    

    Example

    void main() { 
       printMsg(); 
       print(test()); 
    }  
    printMsg()=>
    print("hello"); 
    
    int test()=>123;                       
    // returning function

    It should produce the following output −

    hello 123 
  • Enumeration

    An enumeration is used for defining named constant values. An enumerated type is declared using the enum keyword.

    Syntax

    enum enum_name {  
       enumeration list 
    }
    

    Where,

    • The enum_name specifies the enumeration type name
    • The enumeration list is a comma-separated list of identifiers

    Each of the symbols in the enumeration list stands for an integer value, one greater than the symbol that precedes it. By default, the value of the first enumeration symbol is 0.

    For example

    enum Status { 
       none, 
       running, 
       stopped, 
       paused 
    }

    Example

    enum Status { 
       none, 
       running, 
       stopped, 
       paused 
    }  
    void main() { 
       print(Status.values); 
       Status.values.forEach((v) => print('value: $v, index: ${v.index}'));
       print('running: ${Status.running}, ${Status.running.index}'); 
       print('running index: ${Status.values[1]}'); 
    }

    It will produce the following output −

    [Status.none, Status.running, Status.stopped, Status.paused] 
    value: Status.none, index: 0 
    value: Status.running, index: 1 
    value: Status.stopped, index: 2 
    value: Status.paused, index: 3 
    running: Status.running, 1 
    running index: Status.running 
    
  • Runes

    Strings are a sequence of characters. Dart represents strings as a sequence of Unicode UTF-16 code units. Unicode is a format that defines a unique numeric value for each letter, digit, and symbol.

    Since a Dart string is a sequence of UTF-16 code units, 32-bit Unicode values within a string are represented using a special syntax. A rune is an integer representing a Unicode code point.

    The String class in the dart:core library provides mechanisms to access runes. String code units / runes can be accessed in three ways −

    • Using String.codeUnitAt() function
    • Using String.codeUnits property
    • Using String.runes property

    String.codeUnitAt() Function

    Code units in a string can be accessed through their indexes. Returns the 16-bit UTF-16 code unit at the given index.

    Syntax

    String.codeUnitAt(int index);
    

    Example

    import 'dart:core'; 
    void main(){ 
       f1(); 
    } 
    f1() { 
       String x = 'Runes'; 
       print(x.codeUnitAt(0)); 
    }

    It will produce the following output −

    82
    

    String.codeUnits Property

    This property returns an unmodifiable list of the UTF-16 code units of the specified string.

    Syntax

    String. codeUnits;
    

    Example

    import 'dart:core';  
    void main(){ 
       f1(); 
    }  
    f1() { 
       String x = 'Runes'; 
       print(x.codeUnits); 
    } 

    It will produce the following output −

    [82, 117, 110, 101, 115]
    

    String.runes Property

    This property returns an iterable of Unicode code-points of this string.Runes extends iterable.

    Syntax

    String.runes
    

    Example

    void main(){ 
       "A string".runes.forEach((int rune) { 
    
      var character=new String.fromCharCode(rune); 
      print(character); 
    }); }

    It will produce the following output −

    A 
    s 
    t 
    r 
    i 
    n 
    g
    

    Unicode code points are usually expressed as \uXXXX, where XXXX is a 4-digit hexadecimal value. To specify more or less than 4 hex digits, place the value in curly brackets. One can use the constructor of the Runes class in the dart:core library for the same.

    Example

    main() { 
       Runes input = new Runes(' \u{1f605} '); 
       print(new String.fromCharCodes(input)); 
    }  
  • Symbol

    Symbols in Dart are opaque, dynamic string name used in reflecting out metadata from a library. Simply put, symbols are a way to store the relationship between a human readable string and a string that is optimized to be used by computers.

    Reflection is a mechanism to get metadata of a type at runtime like the number of methods in a class, the number of constructors it has or the number of parameters in a function. You can even invoke a method of the type which is loaded at runtime.

    In Dart reflection specific classes are available in the dart:mirrors package. This library works in both web applications and command line applications.

    Syntax

    Symbol obj = new Symbol('name');  
    // expects a name of class or function or library to reflect 
    

    The name must be a valid public Dart member name, public constructor name, or library name.

    Example

    Consider the following example. The code declares a class Foo in a library foo_lib. The class defines the methods m1, m2, and m3.

    Foo.dart

    library foo_lib;   
    // libarary name can be a symbol   
    
    class Foo {         
       // class name can be a symbol  
       m1() {        
    
      // method name can be a symbol 
      print("Inside m1"); 
    } m2() {
      print("Inside m2"); 
    } m3() {
      print("Inside m3"); 
    } }

    The following code loads Foo.dart library and searches for Foo class, with help of Symbol type. Since we are reflecting the metadata from the above library the code imports dart:mirrors library.

    FooSymbol.dart

    import 'dart:core'; 
    import 'dart:mirrors'; 
    import 'Foo.dart';  
    
    main() { 
       Symbol lib = new Symbol("foo_lib");   
       //library name stored as Symbol 
       
       Symbol clsToSearch = new Symbol("Foo");  
       // class name stored as Symbol  
       
       if(checkIf_classAvailableInlibrary(lib, clsToSearch))  
       // searches Foo class in foo_lib library 
    
      print("class found.."); 
    } bool checkIf_classAvailableInlibrary(Symbol libraryName, Symbol className) { MirrorSystem mirrorSystem = currentMirrorSystem(); LibraryMirror libMirror = mirrorSystem.findLibrary(libraryName);
      
    if (libMirror != null) {
      print("Found Library"); 
      print("checkng...class details.."); 
      print("No of classes found is : ${libMirror.declarations.length}"); 
      libMirror.declarations.forEach((s, d) =&gt; print(s));  
         
      if (libMirror.declarations.containsKey(className)) return true; 
      return false; 
    } }

    Note that the line libMirror.declarations.forEach((s, d) => print(s)); will iterate across every declaration in the library at runtime and prints the declarations as type of Symbol.

    This code should produce the following output −

    Found Library 
    checkng...class details.. 
    No of classes found is : 1 
    Symbol("Foo") // class name displayed as symbol  
    class found. 
    

    Example: Display the number of instance methods of a class

    Let us now consider displaying the number of instance methods in a class. The predefined class ClassMirror helps us to achieve the same.

    import 'dart:core'; 
    import 'dart:mirrors'; 
    import 'Foo.dart';  
    
    main() { 
       Symbol lib = new Symbol("foo_lib"); 
       Symbol clsToSearch = new Symbol("Foo");  
       reflect_InstanceMethods(lib, clsToSearch); 
    }  
    void reflect_InstanceMethods(Symbol libraryName, Symbol className) { 
       MirrorSystem mirrorSystem = currentMirrorSystem(); 
       LibraryMirror libMirror = mirrorSystem.findLibrary(libraryName); 
       
       if (libMirror != null) { 
    
      print("Found Library"); 
      print("checkng...class details.."); 
      print("No of classes found is : ${libMirror.declarations.length}"); 
      libMirror.declarations.forEach((s, d) =&gt; print(s));  
      
      if (libMirror.declarations.containsKey(className)) print("found class");
      ClassMirror classMirror = libMirror.declarations&#91;className]; 
      
      print("No of instance methods found is ${classMirror.instanceMembers.length}");
      classMirror.instanceMembers.forEach((s, v) =&gt; print(s)); 
    } }

    This code should produce the following output −

    Found Library 
    checkng...class details.. 
    No of classes found is : 1 
    Symbol("Foo") 
    found class 
    No of instance methods found is 8 
    Symbol("==") 
    Symbol("hashCode") 
    Symbol("toString") 
    Symbol("noSuchMethod") 
    Symbol("runtimeType") 
    Symbol("m1") 
    Symbol("m2") 
    Symbol("m3")
    

    Convert Symbol to String

    You can convert the name of a type like class or library stored in a symbol back to string using MirrorSystem class. The following code shows how you can convert a symbol to a string.

    import 'dart:mirrors'; 
    void main(){ 
       Symbol lib = new Symbol("foo_lib"); 
       String name_of_lib = MirrorSystem.getName(lib); 
       
       print(lib); 
       print(name_of_lib); 
    }

    It should produce the following output −

    Symbol("foo_lib")   
    
    foo_lib     
    
  • Map

    The Map object is a simple key/value pair. Keys and values in a map may be of any type. A Map is a dynamic collection. In other words, Maps can grow and shrink at runtime.

    Maps can be declared in two ways −

    • Using Map Literals
    • Using a Map constructor

    Declaring a Map using Map Literals

    To declare a map using map literals, you need to enclose the key-value pairs within a pair of curly brackets “{ }”.

    Here is its syntax −

    var identifier = { key1:value1, key2:value2 [,…..,key_n:value_n] }
    

    Declaring a Map using a Map Constructor

    To declare a Map using a Map constructor, we have two steps. First, declare the map and second, initialize the map.

    The syntax to declare a map is as follows −

    var identifier = new Map()
    

    Now, use the following syntax to initialize the map −

    map_name[key] = value
    

    Example: Map Literal

    void main() { 
       var details = {'Usrname':'tom','Password':'pass@123'}; 
       print(details); 
    }

    It will produce the following output −

    {Usrname: tom, Password: pass@123}
    

    Example: Adding Values to Map Literals at Runtime

    void main() { 
       var details = {'Usrname':'tom','Password':'pass@123'}; 
       details['Uid'] = 'U1oo1'; 
       print(details); 
    } 

    It will produce the following output −

    {Usrname: tom, Password: pass@123, Uid: U1oo1}
    

    Example: Map Constructor

    void main() { 
       var details = new Map(); 
       details['Usrname'] = 'admin'; 
       details['Password'] = 'admin@123'; 
       print(details); 
    } 

    It will produce the following output −

    {Usrname: admin, Password: admin@123}
    

    Note − A map value can be any object including NULL.

    Map – Properties

    The Map class in the dart:core package defines the following properties −

    Sr.NoProperty & Description
    1KeysReturns an iterable object representing keys
    2ValuesReturns an iterable object representing values
    3LengthReturns the size of the Map
    4isEmptyReturns true if the Map is an empty Map
    5isNotEmptyReturns true if the Map is an empty Map

    Map – Functions

    Following are the commonly used functions for manipulating Maps in Dart.

    Sr.NoFunction Name & Description
    1addAll()Adds all key-value pairs of other to this map.
    2clear()Removes all pairs from the map.
    3remove()Removes key and its associated value, if present, from the map.
    4forEach()Applies f to each key-value pair of the map.
  • Lists (Basic Operations)

    In this chapter, we will discuss how to carry out some basic operations on Lists, such as −

    Sr.NoBasic Operation & Description
    1Inserting Elements into a ListMutable Lists can grow dynamically at runtime. The List.add() function appends the specified value to the end of the List and returns a modified List object.
    2Updating a listLists in Dart can be updated by −Updating The IndexUsing the List.replaceRange() function
    3Removing List itemsThe following functions supported by the List class in the dart:core library can be used to remove the item(s) in a List.