Dart : Redirecting constructors

Dart : Redirecting constructors

·

1 min read

Redirecting constructors

  • Sometimes a constructor's only purpose is to redirect to another constructor in the same class. A redirecting constructor's body is empty, with the constructor call (using this instead of the class name) appearing after a colon (:).

  •   class Point {
        double x, y;
    
        // The main constructor for this class.
        Point(this.x, this.y);
    
        // Delegates to the main constructor.
        Point.alongXAxis(double x) : this(x, 0);
      }