Junior — Middle
How is a forwarding constructor implemented in a programming language?
sobes.tech AI
Answer from AI
In Flutter (Dart), a redirecting constructor allows one constructor to call another to avoid code duplication. This is done using a colon and the name of the other constructor after the parameter list.
Example:
class Point {
final int x, y;
Point(this.x, this.y);
// Redirecting constructor that creates a point on the X axis
Point.alongXAxis(int x) : this(x, 0);
}
void main() {
var p = Point.alongXAxis(5);
print('x: ${p.x}, y: ${p.y}'); // x: 5, y: 0
}