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 Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created in the program. In object - oriented programming, as soon as we create an object, the compiler automatically invokes the constructor, this constructor is known as the default constructor. All classes have their default constructor which is created by the compiler when class is called, moreover one can also define their own constructor explicitly. In that case, the default constructor will be ignored and the compiler won’t invoke it.

Remember that the constructors have the same name as the class name and don’t have any return type.

Consider the following syntax of creating a constructor :

Syntax :

class_name( < parameters > )

{

   // Body of the constructor

}

In the above syntax, ‘class_name’ refers to the name of the class whose constructor is being created.

‘<parameters>’ refer to the list of arguments that are passed to the constructor. However, it is an optional feature to pass parameters to the constructor. The default constructor has no parameter defined in it.

The body of the constructor refers to the lines of code that defines the functioning of the constructor. The constructor is called when an object is created.

Constructors don’t have any return type and therefore, we did not mention anything related to the return type in the syntax as well.

Program

Consider the following code in Dart that explains the concept of constructors

// Defining the class student

class student

{

  /* defining the constructor of the class student

   * with the same name as of the class, and no r

   * return type */

student( )

{

    /* Everytime the constructor is invoked, this

     * line is printed */

    print( ' Constructor is being invoked ! ' ) ;

  }

   

  // initialzing a string str

  String str = ' NULL ' ;

   

  // Creating Function inside class

  void display(){

    print( " The string is $str " ) ;

  }

}

  void main( )

{

  // Creating an object s1 of the class student

  student s1 = new student( );

   

  /* Accessing the instance variable str using

   * the object and intializing it with some

   * value */

  s1.str = ' This tutorial explains constructor also ! ' ;

   

  /* Accessing the instance function display using

   * the object to display the value of string */

  s1.display( ) ;

}

Output :

Constructor is being invoked !

The string is  This tutorial explains constructor also !

There are three types of constructors in Dart :

  1. Default Constructor
  2. Parametrized Constructor
  3. Named Constructor

Default Constructor :

As mentioned above, every class has a default constructor that does not have any parameter or return type. It is invoked by the compiler automatically even if we do not declare or define it explicitly.

Syntax :

class ClassName { 

   ClassName( )

  // body of the constructor

}

Consider the following code in Dart that explains the concept of default constructor

Example :

/* A program to illustrate the concept of

* default constructor.

* Consider the definition of the class student */

class student

{

  // default constructor of the class

  student( )

{

    print( ' This is the default constructor ' ) ;

  }

}

  void main( )

{

      // declaring an object of the class

  student s1 = new student( ) ;

}

Output :

This is the default constructor

Explanation :

Even though we did not explicitly call any constructor, the compiler invokes it automatically.

Parameterized Constructor :

In Dart, the constructors can also have parameters, such constructors are called parameterized constructor. It is created to initialize the objects with some different values. These parameters will decide which constructor will be called and which will be not. A constructor can have single or multiple parameters. 

Syntax :

class ClassName { 

   ClassName( parameter_list )

  // body of the constructor 

}

Consider the following code in Dart that explains the concept of parameterized constructor

Example : 

 /* A program to illustrate the concept of

* parameterized constructor.

* Consider the definition of the class student */

class student

{

  // parameterized constructor of the class

  student( int a )

  {

          print( ' This is the parameterized constructor ' ) ;

          a = 5 ;

          print( ' Value of a is : ' ) ;

          print( a ) ;

  }

}

void main( )

{

  // Creating an object s1 of the class student

  student s1 = new student( 5 ) ;

}

Output :

 This is the parameterized constructor

Value of a is :

5

Note : You can’t have two constructors with the same name although they have different parameters. The compiler will display an error.

Named Constructor :

As you can’t define multiple constructors with the same name, this type of constructor is the solution to that problem. They allow the user to make multiple constructors with a different name.

Syntax :

class_name.constructor_name ( parameters ) {

   // Body of the Constructor

}

Consider the following code in Dart that explains the concept of named constructor

Example :

/* A program to illustrate the concept of

* named constructor.

* Consider the definition of the class student */




class student

{

   /* creating the named constructor with a single

    * parameter a. Thus, it is a parameterized

    * constructor as well. */

  student.constructor1( int a )

  {

    print( ' This is the parameterized constructor with only one parameter : $a ' ) ;

  }

   

  /* creating the named constructor with two

   * parameters a and b. Thus, it is a parameterized

   * constructor as well. */

  student.constructor2( int a, int b )

  {

    print( ' This is the parameterized constructor with two parameters ' ) ;

    print( ' Value of a + b is ${ a + b } ' ) ;

  }

}

  void main( ) {

  // creating two objects s1 and s2 of class student

  student s1 = new student.constructor1( 5 ) ;

  student s2 = new student.constructor2( 7, 8 ) ;

}

Output :

This is the parameterized constructor with only one parameter : 5

This is the parameterized constructor with two parameters

Value of a + b is 15