TExcelTAD
Home › Flutter › Dart Constructors

Dart Constructors

A constructor creates and initializes an object. Dart has an unusually rich set of constructor features — and Flutter widgets use nearly all of them. Once you can read const MyButton({super.key, required this.label, this.color = Colors.blue}), widget code will feel natural.

Generative constructors and initializing formals

The simplest constructor has the same name as the class. this.field in the parameter list (an initializing formal) assigns the argument directly to the field.

dart · try it yourself
class Rectangle {
  final double width;
  final double height;

  Rectangle(this.width, this.height);

  double get area => width * height;
}

void main() {
  var r = Rectangle(3, 4);
  print('Area: ${r.area}');
}
Area: 12.0

Note that Rectangle(3, 4) passes int literals where double is expected — Dart treats integer literals as doubles in a double context.

Named parameters in constructors

Flutter widgets almost always use named constructor parameters. Combine them with required and default values:

dart · try it yourself
class Button {
  final String label;
  final String color;
  final bool enabled;

  Button({required this.label, this.color = 'blue', this.enabled = true});

  @override
  String toString() => 'Button($label, $color, enabled: $enabled)';
}

void main() {
  print(Button(label: 'OK'));
  print(Button(label: 'Delete', color: 'red'));
  print(Button(enabled: false, label: 'Wait'));
}
Button(OK, blue, enabled: true)
Button(Delete, red, enabled: true)
Button(Wait, blue, enabled: false)

Initializer lists and assertions

An initializer list runs before the constructor body. It follows a colon and can compute final fields or check arguments with assert.

dart · try it yourself
class Circle {
  final double radius;
  final double diameter;

  Circle(this.radius)
      : assert(radius >= 0),
        diameter = radius * 2;
}

void main() {
  var c = Circle(1.5);
  print('r=${c.radius} d=${c.diameter}');
}
r=1.5 d=3.0

Named constructors

A class can have several constructors with different names: ClassName.name(...). They describe how the object is created. Flutter examples include EdgeInsets.all, EdgeInsets.symmetric, ListView.builder and Image.network.

dart · try it yourself
class Color {
  final int r, g, b;

  const Color(this.r, this.g, this.b);
  const Color.gray(int level) : this(level, level, level);
  const Color.black() : this(0, 0, 0);

  @override
  String toString() => 'rgb($r, $g, $b)';
}

void main() {
  print(Color(255, 128, 0));
  print(Color.gray(200));
  print(Color.black());
}
rgb(255, 128, 0)
rgb(200, 200, 200)
rgb(0, 0, 0)

: this(...) redirects to another constructor so the initialization logic lives in one place.

const constructors

If all fields are final, the constructor can be const. Objects created with const are canonicalized: identical const expressions produce the very same object. That's why Flutter recommends const widgets — the framework can skip rebuilding them.

dart · try it yourself
class Spacing {
  final double value;
  const Spacing(this.value);
}

void main() {
  const a = Spacing(8);
  const b = Spacing(8);
  final c = Spacing(8);

  print(identical(a, b));
  print(identical(a, c));
}
true
false

Factory constructors

A factory constructor doesn't have to create a new object — it can return a cached instance or a subtype. The most common use in Flutter apps is fromJson:

dart · try it yourself
class User {
  final String name;
  final int age;

  User(this.name, this.age);

  factory User.fromJson(Map<String, dynamic> json) {
    return User(json['name'] as String, json['age'] as int);
  }
}

void main() {
  var u = User.fromJson({'name': 'Vy', 'age': 22});
  print('${u.name}, ${u.age}');
}
Vy, 22

Super parameters

When a subclass passes constructor arguments to its parent, Dart 3 lets you write super.name directly in the parameter list instead of forwarding them by hand. Every Flutter widget does this with key:

dart
class Greeting extends StatelessWidget {
  const Greeting({super.key, required this.name});
  final String name;
  // ...
}

super.key means "accept a key parameter and pass it to StatelessWidget's constructor". Before Dart 2.17 you had to write Greeting({Key? key, required this.name}) : super(key: key);.

Constructor cheat sheet

Form Example
Generative Point(this.x, this.y);
Named parameters Point({required this.x, this.y = 0});
Initializer list Point(this.x) : y = x * 2;
Named constructor Point.origin() : x = 0, y = 0;
Redirecting Point.diag(int v) : this(v, v);
Const const Point(this.x, this.y);
Factory factory Point.fromJson(Map j) => ...;
Super parameters const MyWidget({super.key});

Test yourself

What does this.width in a constructor's parameter list do?

  1. Declares a new local variable
  2. Assigns the argument to the width field
  3. Calls the width getter
  4. Makes width static

Which constructor type is typically used for fromJson?

  1. const
  2. factory
  3. static
  4. redirecting only

What does super.key in const MyWidget({super.key}) mean?

  1. Reads the parent's key field
  2. Passes the key argument through to the superclass constructor
  3. Creates a new Key
  4. Makes key required