Dart this

Summary: in this tutorial, you’ll learn about what Dart this keyword is and how to use it more effectively.

What is Dart this keyword

A class is a blueprint of objects. To reference the current object inside a class, you use the this keyword. The this keyword means the current instance of the class.

The following defines the Point class with two properties x and y and two methods move() and show():

class Point {
  int x = 0;
  int y = 0;

  void move(int x1, int y1) {
    x = x1;
    y = y1;
  }

  void show() {
    print('Point($x,$y)');
  }
}Code language: Dart (dart)

To reference the properties x and y inside a method, you can directly use their names such as x and y.

Also, you can reference these properties using the this keyword. For example:

this.x
this.yCode language: Dart (dart)

This is useful when the parameters of the method have the same names as the properties, for example:

class Point {
  int x = 0;
  int y = 0;

  void move(int x, int y) {
    this.x = x;
    this.y = y;
  }

  void show() {
    print('Point($x,$y)');
  }
}Code language: Dart (dart)

In this example, we change the parameters of the method move() from x1 and y1 to x and y. Therefore, we have to use the this keyword to explicitly reference x and y properties:

void move(int x, int y) {
  this.x = x;
  this.y = y;
}Code language: Dart (dart)

Using Dart this keyword for method chaining

The following example adds a new method reset() to the Point class. The reset() method resets the xand y coordinate of the Point object:

class Point {
  int x = 0;
  int y = 0;

  void move(int x, int y) {
    this.x = x;
    this.y = y;
  }

  void reset() {
    this.x = 0;
    this.y = 0;
  }

  void show() {
    print('Point($x,$y)');
  }
}Code language: Dart (dart)

The following creates a new instance of the Point class and successively calls the move(), show(), and reset() methods:

class Point {
  int x = 0;
  int y = 0;

  void move(int x, int y) {
    this.x = x;
    this.y = y;
  }

  void reset() {
    this.x = 0;
    this.y = 0;
  }

  void show() {
    print('Point($x,$y)');
  }
}

void main() {
  var p1 = Point();
  p1.move(10, 20);
  p1.show();
  p1.reset();
}Code language: Dart (dart)

It would be less verbose if we can call the methods of the p1 object like this:

 p1.move(10, 20).show().reset();Code language: Dart (dart)

This is called method chaining. And the this keyword allows you to implement it like this:

class Point {
  int x = 0;
  int y = 0;

  Point move(int x, int y) {
    this.x = x;
    this.y = y;
    return this;
  }

  Point reset() {
    this.x = 0;
    this.y = 0;
    return this;
  }

  Point show() {
    print('Point($x,$y)');
    return this;
  }
}

void main() {
  var p1 = Point();
  p1.move(10, 20).show().reset();
}Code language: Dart (dart)

In this example, we change the return type of the move(), show(), and reset() methods to Point class and return the this keyword inside these methods.

The following method call returns the p1 object:

p1.move(10, 20)Code language: Dart (dart)

Therefore, we can call the show() method immediately instead of reassigning it back to p1:

p1.move(10, 20).show()Code language: Dart (dart)

In turn, the show() method returns the p1 object. Hence, we can call the reset() method immediately:

 p1.move(10, 20).show().reset();Code language: Dart (dart)

Summary

  • The Dart this keyword reference the current object of the class.
  • Use Dart this keyword for method chaining to make the code more concise.
Was this tutorial helpful ?