Class

In Dart, everything is an object. And each object belongs to its corresponding class. Each class is a subclass of Object class. A class in dart contains several things, like:

Defining a Class

To create a class, use class keyword, followed by the name of the class. And then the body of the class follows.

class className{
	// body of the class
}

For example:

class Point{
	num x;
	num y;
}

Now you can create object of the class Point and get access to it's properties x and y. The variable x and y is called instance variable or properties.

Instance variables

Here’s how you declare instance variables:

class Point {
  num x; // Declare instance variable x, initially null.
  num y; // Declare y, initially null.
  num z = 0; // Declare z, initially 0.
}

All uninitialized instance variables have the value null.

Constructor

Constructors are of two types:

Unnamed Constructor

Declare a unnamed constructor by creating a function with the same name as its class. Here is an example:

class Point {
  num x, y;

  Point(num x, num y) {
    // There's a better way to do this, stay tuned.
    this.x = x;
    this.y = y;
  }
}

The this keyword refers to the current instance.

The pattern of assigning a constructor argument to an instance variable is so common, Dart has syntactic sugar to make it easy:

class Point {
  num x, y;

  // Syntactic sugar for setting x and y
  // before the constructor body runs.
  Point(this.x, this.y);
}

Default Constructor

If you don’t declare a constructor, a default constructor is provided for you. The default constructor has no arguments and it is a unnamed constructor, which means the default constructor has the same name of the class name.

Constructors are not Inherited

Subclasses don’t inherit constructors from their superclass. A subclass that declares no constructors has only the default constructor.

Named Constructor

Use a named constructor to implement multiple constructors for a class or to provide extra clarity:

class Point {
  num x, y;

  Point(this.x, this.y);

  // Named constructor
  Point.origin() {
    x = 0;
    y = 0;
  }
}

Here we have a named constructor Point.origin().

Remember that constructors are not inherited, which means that a superclass’s named constructor is not inherited by a subclass.

Instance Methods

Instance methods on objects can access instance variables and this.

import 'dart:math';

class Point {
  num x, y;

  Point(this.x, this.y);

  num distanceTo(Point other) {
    var dx = x - other.x;
    var dy = y - other.y;
    return sqrt(dx * dx + dy * dy);
  }
}

Here we have defined an instance method, called distanceTo().

Using instance members

Objects have members consisting of functions and data (methods and instance variables, respectively). When you call a method, you invoke it on an object: the method has access to that object’s functions and data. Use a dot (.) to refer to an instance variable or method:

var p = Point(2, 2);

// Set the value of the instance variable y.
p.y = 3;

// Get the value of y.
assert(p.y == 3);

// Invoke distanceTo() on p.
num distance = p.distanceTo(Point(4, 4));

Use ?. instead of . to avoid an exception when the leftmost operand is null:

// If p is non-null, set its y value to 4.
p?.y = 4;

Using constructors

You can create an object using a constructor. Constructor names can be either ClassName or ClassName.identifier. The second form of the constructor is called Named Constructor. For example, the following code creates Point objects using the Point() and Point.fromJson() constructors:

var p1 = Point(2, 2);
var p2 = Point.fromJson({'x': 1, 'y': 2});

The following code has the same effect, but uses the optional new keyword before the constructor name:

var p1 = new Point(2, 2);
var p2 = new Point.fromJson({'x': 1, 'y': 2});

The new keyword became optional in Dart 2.

Private instance Members

Use _(Underscore) before the member name to make it private to the class itself. The private member can be accessible by the class method itself. You cannot access them from its object.

class Person{
  final string _id; // Private property
  string name;

  Person._internal(this.name); // private named constructor

  void _generateId(){ // A private method
    // Code --
  }
}

Summary

In this section we have learned the following:

In the next section we will explore various parts of the class.