The Class is one of the object-oriented concepts in Dart language.

Since Dart is an object-oriented language, It provides each functionality and feature as classes and objects.

The class is a blueprint of objects.

The class contains the following components syntax.

class{
  // instance member variables

  constructor(){

  }
  // setters and getters
  // functions and methods
}

How to create a class in dart?

Classes in dart are created with the class keyword.

class Employee {
  String name = "john";
}

In the above example, the Employee class is created with a named variable containing a string.

How to create an object of a class

The object is a blueprint of a class.

The object can be created and used in other classes.

Declare a variable name for the type object

the object can be created using the optional new keyword and optional class name for variable declaration.

Following is an example of object creation in multiple ways.

main() {
  // use var and classname with ()
  var employee = Employee();
  // use var and new keyword and classname with ()
  var employee1 = new Employee();
  // use classname and new keyword and classname with ()

  Employee employee2 = new Employee();
  // use classname andclassname with ()
  Employee employee3 = Employee();

  print(employee);
  print(employee.runtimeType);
}

class Employee {
  String name = "john";
}

Dart Class constructor

The class contains a constructor, which is a special function created with the same name of a class with or without arguments.

It is used to initialize a data of an object created from a class. That means It is called when the object is created. Syntax:

class Class1 {  
     Class1() {  }

}  

Notes:

  • constructor is called automatically during object creation
  • It is used to initialize a data of an object
  • Name of the constructor and classname should match
  • Optional Arguments, if arguments are provided, Object creation should be passed with arguments.
  • The return type is not required

Here is a class constructor example

main() {
  var employee = Employee("Andrew");
  var employee1 = Employee("Mark");

  print(employee.name);// Andrew
  print(employee1.name); // Mark

}

class Employee {
  String name = "john";
  Employee(String name) {
    this.name = name;
  }
}

Constructors Contain arguments of different types

  • Optional Arguments with or without parameter

The above example contains a parameterized constructor. If the constructor is not provided, Then it is called default constructor.

Only one of the constructors from default or parameter constructor must be defined.

  • Named constructor

Dart does not allow multiple constructors of default or parameterized constructor.

It provided a named constructor with a name with passing arguments.

Here is an example parameterized constructor class in dart.

main() {
  var employee = Employee(); // default constrcutor called
  var employee1 = Employee.changeNameTitle("Mark"); // Named constrcutor called

  print(employee.name);
  print(employee1.name);
}

class Employee {
  String name = "john";
  Employee() {}

  Employee.changeNameTitle(String name) {
    this.name = "Hello "+ name;
  }
}

Dart class instance variables

The class contains the state of data represented in instance variable properties.

In the below example, Class contains a name instance variable

class Employee {
  String name = "john";
}

instance variables can be accessed using the object. instance variable.

main() {
  // use var and classname with ()
  var employee = Employee();


  print(employee);
  print(employee.name); //john
}

Dart class methods

Dart class methods that contain the functionality to do or modify the state(instance variables).

These are normal functions that contain function name, body arguments, and return type.

Syntax:

returntype functionName(arguments){
  //body
}

Here is a class functions example In this example, the captilizeName function capitalizes the first letter of a name.

main() {
  var employee = Employee(); // default constrcutor called
  var employee1 = Employee.changeNameTitle("mark"); // Named constrcutor called

  print(employee.captilizeName());
  print(employee1.captilizeName());
}

class Employee {
  String name = "john";
  Employee() {}

  Employee.changeNameTitle(String name) {
    this.name = "Hello " + name;
  }

  String captilizeName() {
    return name[0].toUpperCase() + name.substring(1);
  }
}

Dart class setter and getter method example

getter and setter are methods in a class used to read and modify the instance variables in dart.

getter() method read the instance attribute of a class setter() method update the instance variable value to new value.

  • Setter and getter method syntax

Syntax

datatype get   get[Variable]{
  //return statement
}


set   set[Variable](argments){
  //return statement
}

Here is an example setter and getter example in dart class

main() {
  var employee = Employee(); // default constructor called

  employee.setName = "john";
  print(employee.getName);
}

class Employee {
  String name = "john";
  Employee() {}

  set setName(String name) {
    this.name = name;
  }

  String get getName {
    return this.name;
  }
}

There are some differences with the java language. getter has no braces() in the declaration To call setter, you must assign the value