Sub topic 8
Classes and Constructors
Assume you already understand variables, functions, and basic object-oriented ideas such as classes and objects. In Dart, a class defines the data and behavior that its objects share. A constructor creates and initializes an object.
Defining a class
A class usually contains fields and methods. A field stores data. A method is a function that belongs to the class.
The final fields below must be assigned exactly once. The constructor assigns them when a Point is created.
import 'dart:math';
class Point {
final double x;
final double y;
const Point(this.x, this.y);
const Point.origin() : this(0, 0);
double distanceTo(Point other) {
final dx = x - other.x;
final dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}
}
void main() {
const a = Point(3, 4);
print(a.distanceTo(const Point.origin()));
}Point(this.x, this.y) is a shorthand constructor. Dart takes the constructor parameters and stores them in fields with the same names. The longer equivalent is:
Point(double x, double y)
: x = x,
y = y;The constructor body runs after the fields are initialized. A field marked final cannot be changed later, which makes the object immutable after construction.
Constructor forms
Dart supports several useful constructor forms.
| Form | Example | Use |
|---|---|---|
| Generative | `Point(this.x, this.y)` | Creates and initializes an instance |
| Named | `Point.origin()` | Gives a constructor a descriptive name |
| Constant | `const Point(0, 0)` | Creates a compile-time constant |
| Redirecting | `Point.origin() : this(0, 0)` | Reuses another constructor |
A const constructor requires every instance field to be initialized with constant values. You can call it with const when the arguments are also constant. Constant objects can be reused safely because their state cannot change.
A named constructor does not have to be a special version of the default constructor. It can perform different validation or initialization. A redirecting constructor is useful when several constructors should share one initialization path.
Methods and object state
Methods can read the current object's fields through implicit this. Use this when a parameter and a field have the same name, or when it makes the code clearer.
Keep related behavior inside the class. For example, distanceTo knows how a point's coordinates determine its distance. Code using Point does not need to repeat that calculation.
A class can also validate constructor arguments with an initializer-list assertion:
class Temperature {
final double celsius;
Temperature(this.celsius) : assert(celsius >= -273.15);
}The assertion documents and checks the valid range during development. For validation that must run in production, throw an exception instead.
Object creation flow
Use a regular constructor for runtime values. Use a const constructor when the object and all its inputs are known at compile time.
Exercise
Add a named constructor called Point.unitX that creates a point at (1, 0), then print its distance from the origin.
Show answer
Write const Point.unitX() : this(1, 0); inside Point, then call print(const Point.unitX().distanceTo(const Point.origin()));.