Factory and const Constructors
Cache, return subtypes, or build compile-time objects.
Factory and const Constructors is a free Dart Academy lesson on CoddyKit — lesson 4 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.
What a factory Constructor Is
A factory constructor looks like a normal one but you control what it returns, so it does not have to build a brand-new object every time.
class Logger {
factory Logger() => _shared;
}factory Can Return a Cached Object
Because it returns rather than always creating, a factory can hand back a cached instance, perfect for singletons or pools.
class Logger {
static final _it = Logger._();
Logger._();
factory Logger() => _it;
}factory Can Return a Subtype
A factory may return any object that is the right type, including a subclass, letting you pick the concrete class at runtime.
factory Shape(String k) =>
k == "box" ? Box() : Dot();factory Must Return
Unlike a normal constructor, a factory has a body that must return an instance; it never sets fields with this. directly.
factory User.fromJson(Map j) {
return User(j["name"]);
}A Common Parsing Factory
The factory pattern shines for parsing, like building an object from a map. The fromJson idiom is everywhere in Dart and Flutter.
factory User.fromJson(Map<String, dynamic> j) =>
User(j["name"]);What a const Constructor Is
A const constructor lets you build compile-time constant objects whose fields are all final and known ahead of time.
class Point {
final int x, y;
const Point(this.x, this.y);
}Creating const Instances
Call it with the const keyword to make a frozen object. Two const objects with equal values are the very same instance.
const a = Point(1, 2);
const b = Point(1, 2);
print(identical(a, b)); // trueRules for const Constructors
Every field must be final, and the constructor body must be empty. Those rules are what let Dart freeze the object at compile time.
class Color {
final int v;
const Color(this.v);
}const Saves Memory
Identical const objects are canonicalized into one shared instance, so reusing them costs nothing extra in memory.
const reds = [Color(255), Color(255)];factory vs const at a Glance
Reach for factory when you decide what to return at runtime; reach for const when you want immutable, compile-time values.
Combining Ideas
A class can offer a normal constructor, a factory for parsing, and a const one for fixed values, covering every creation need.
Quick Check
Quick check on factory constructors.
Recap: factory and const
You learned that factory constructors control what they return, while const constructors create shared, immutable compile-time objects. Great work! 🎉
Frequently asked questions
Is the “Factory and const Constructors” lesson free?
Yes — the full text of “Factory and const Constructors” 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 “Factory and const Constructors”?
Cache, return subtypes, or build compile-time objects. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Factory and const Constructors” 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