Skip to content
Justice Tech School

Dart Constructors

Named constructors give extra ways to create objects, and factory constructors can return existing instances. const constructors build compile time constants.

Example
class Point {
  final int x, y;
  const Point(this.x, this.y);
  Point.origin() : x = 0, y = 0;
}
void main() {
  print(Point.origin().x);
  print(const Point(1, 2).y);
}

Exercise

Named constructor form.

Point.() : x = 0

Quiz Test

What is a named constructor?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.