• Guides
    • Getting started with Dart
  • Blog
  • FAQ
    • Dart
    • Flutter
  • Contact
  • Guides
    • Getting started with Dart
  • Blog
  • FAQ
    • Dart
    • Flutter
  • Contact
  • Getting Started with Dart
    • Dart Variables
    • Dart Operators
    • Dart Comments
  • Dart Data Types
    • Dart Strings
    • Dart Numbers
    • Dart Booleans
    • Dart Lists
    • Dart Maps
  • Control Flow
    • Dart if...else Statements
    • Dart switch Statement
    • Dart for Loop
    • Dart for...in Loop
    • Dart while and do...while Loops
    • break and continue Statements
  • Dart Functions
  • Dart Classes
  • Dart Objects
  • Dart Constructors
  • Dart Exceptions

Dart Constructors

201 views 1

Written by Ajaya Shrestha
January 26, 2022

In this chapter, we will learn about Dart constructors and how it is used within the class with the help of examples.


Dart Constructors

Constructors are used inside the class.

Constructors are a function that initializes an object when it is created.

When an object is created, the constructor is called by default.

For example,

If we have a class Person and we create an object

var student = new Person();

This invokes the default constructor of the Person class.


Creating Constructors

The syntax of creating constructors is:

class Class_Name {
    Class_Name() {
        // constructor body
    }
}

Here, the first Class_Name is the class.

Class_Name() is a constructor

The constructor is defined with the same name as of the class.

Example: Creating Constructors

void main() { 
   var student = new Person('Sam'); 
} 
class Person { 
   Person(String name) {
     print("Name : ${name}");
   } 
}

Output

Name : Sam

In the above example, class Person is a class.

Person(String name) is a constructor that takes the string parameter. And the value is printed inside the constructor.

student is an object that passes a value Sam.

var student = new Person('Sam');

Constructors Types

There are three types of Dart constructors. They are:

  • Default Constructor
  • Parameter Constructor
  • Named Constructor

Default Constructor

A default constructor has no parameter. If a constructor is not declared in a class, a default no-argument constructor is provided by the Dart compiler.

If a constructor is available, the compiler ignores the default constructor.

The syntax of default constructor is:

class Class_Name {
    Class_Name() {
        // constructor body
    }
}

Example: Default Constructor

void main() { 
   var student = new Person(); 
} 
class Person { 
   Person() {
     print("Default constructor example.");
   } 
}

Output

Default constructor example.

The above example shows the default constructor with no parameters.


Parameter Constructor

The parameter constructor accepts single or multiple parameters. The parameters initialize the instance variables.

The syntax of parameter constructor is:

class Class_Name {
    Class_Name(parameters) {
        // constructor body
    }
}

Example: Parameter Constructor

void main() { 
   var student1 = new Person('Sam', 22);
   var student2 = new Person('Jerry', 23); 

} 
class Person { 
   Person(String name, int age) {
     print("Name : ${name}");
     print("Age: ${age}");
   } 
}

Output

Name : Sam
Age: 22
Name : Jerry
Age: 23

Name : Sam

Age: 22

Name : Jerry

Age: 23

In the above example, the constructor takes two parameters name and age.


Named Constructor

We can also use multiple constructors inside a single class through a named constructor.

The syntax of named constructor is:

className.constructor_name(param_list);

Example: Named Constructor

void main() {
   // object
   var student1 = new Person();
   var student2 = new Person.namedConst("Sam");
} 
class Person {
   // constructor
   Person() {
     print("Named constructor example.");
   }
  
   //second constructor
   Person.namedConst(String name) {
     print("Name is: ${name}");
    }
}

Output

Named constructor example.
Name is: Sam

In the above example, the second constructor takes the name parameter.

Person.namedConst(String name) {
    print("Name is: ${name}");
}

Was this helpful?

1 Yes  2 No
Related Articles
  • Dart Exceptions
  • Dart Objects
  • Dart Classes
  • Control Flow
  • Dart Functions
  • break and continue Statements
Previously
Dart Objects
Up Next
Dart Exceptions
flutter-dev-experts




FLUTTERGUIDE
FlutterGuide is an easy learning guide platform to help you build apps for any screen. The leading Dart/Flutter guides for getting started and become pro.
Guides
  • Getting started with Dart 22
  • Private Documentation 1
Categories
  • Firebase
  • Dart
  • iOS
  • API
  • Bloc
  • Flutter
  • Widgets
  • Mobile
  • State Management
  • Design
  • Animations
  • Flutter Designs
About
About Us
Guides
Blogs
FAQs
Contact
  • Privacy Policy
  • Terms & Conditions
  • Copyright 2022 Fawesome Apps. All Rights Reserved