BNetz
BNetz

Reputation: 361

Flutter/Dart: When is the keyword this used?

I know how to use the keyword this, but I am not sure if there are situations where it has to be avoided for some reasons. Example from dart.dev, chapter constructors:

class Point {
  double x, y;

  Point(this.x, this.y);

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

Why don't they write

this.x = 0;

instead of

x = 0;

?

Upvotes: 0

Views: 433

Answers (1)

Kirill Bubochkin
Kirill Bubochkin

Reputation: 6343

In this case you can write this.x as well, it's just a matter of style.

Generally, when there's no ambiguity, it's recommended to skip this as it's invoked implicitly.

Upvotes: 4

Related Questions