Named Constructors for Clear Intent
Offer multiple ways to construct.
Named Constructors for Clear Intent is a free Dart Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Named Constructors
One class often needs several ways to be built. A named constructor gives each path a clear label so callers know exactly what they get.
The Naming Syntax
Write the class name, a dot, then your label. The form ClassName.label reads like a tiny sentence at the call site.
class Point {
int x, y;
Point.origin() : x = 0, y = 0;
}Calling a Named Constructor
You invoke it just like the unnamed one, but you include the label. This makes the intent of the object obvious in the code.
var p = Point.origin();
print(p.x); // 0Many Named Constructors
Add as many as you like, each describing a use case. Here one builds an empty cart and another a preset one.
class Cart {
int items;
Cart.empty() : items = 0;
Cart.preset() : items = 3;
}Named Constructors With Parameters
They can take arguments too, so you mix a clear label with custom values for a flexible yet readable factory-style entry point.
class User {
String name;
User.guest(this.name);
}Pairing Named With Unnamed
A class can keep its normal unnamed constructor and add named ones beside it, covering both the common and the special cases.
class Color {
int value;
Color(this.value);
Color.black() : value = 0;
}Modeling Different Sources
Named constructors shine when the same data arrives from different places, like a JSON map or raw values, each with its own label.
class User {
String name;
User.fromName(this.name);
}Readability at the Call Site
Compare Temperature.celsius(20) with a bare number. The label removes guesswork, so named constructors boost clarity for readers.
var t = Temperature.celsius(20);They Still Build the Same Type
No matter which constructor you call, you get an instance of the same class. The label only changes how it is initialized, not its type.
var a = Point.origin();
print(a is Point); // trueCombine With this. Shortcuts
Named constructors accept the same this.field shorthand, keeping your initialization concise even with a descriptive label.
class Box {
int w, h;
Box.square(this.w) : h = w;
}Choose Descriptive Labels
Pick labels that describe the result, like fromMap or withDefaults. A good label turns the call site into documentation.
class Config {
Config.withDefaults();
Config.fromMap(Map m);
}Quick Check
Quick check on named constructor syntax.
Recap: Clear Intent
You learned to add named constructors with the dot syntax, mix them with the unnamed one, and pass parameters for readable, intent-revealing object creation. 👍
Frequently asked questions
Is the “Named Constructors for Clear Intent” lesson free?
Yes — the full text of “Named Constructors for Clear Intent” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “Named Constructors for Clear Intent”?
Offer multiple ways to construct. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Named Constructors for Clear Intent” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Default and Parameterized Constructors
- Named Constructors for Clear Intent
- Initializer Lists and Redirecting
- Factory and const Constructors