Initializer Lists and Redirecting
Set finals and reuse constructors.
Initializer Lists and Redirecting is a free Dart Academy lesson on CoddyKit — lesson 3 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.
Setting final Fields
A final field must be set before the constructor body runs. The initializer list is the perfect place to give it its one value.
class Circle {
final double area;
Circle(double r) : area = 3.14 * r * r;
}The Initializer List Syntax
After the parameter list, add a colon and comma-separated assignments. This initializer list runs before the constructor body.
Point(int x, int y) : x = x, y = y;Runs Before the Body
Initializer assignments happen first, then the body executes. So fields you set in the list are already ready inside the body.
class Box {
final int n;
Box(int v) : n = v {
print(n); // available
}
}Computing Derived Values
Use the list to compute a field from the parameters, such as a total or a label, before anyone can touch the object.
class Rect {
final int area;
Rect(int w, int h) : area = w * h;
}Multiple Initializers
Separate several assignments with commas. Each one sets a different field in the same single initializer list.
Point(int a, int b) : x = a, y = b;assert in the Initializer List
You can drop an assert into the list to guard inputs during development, catching bad values before the object exists.
Circle(double r) : assert(r > 0), area = 3.14 * r * r;Redirecting Constructors
One constructor can hand off to another using this after the colon, so shared setup lives in a single place.
class Point {
int x, y;
Point(this.x, this.y);
Point.zero() : this(0, 0);
}Why Redirect
Redirecting avoids repeating field assignments. The named one just calls the main one, keeping your logic DRY.
Point.origin() : this(0, 0);Redirect Bodies Stay Empty
A redirecting constructor has no body. It only forwards to the target with this(...), which does all the real work.
class User {
String name;
User(this.name);
User.anon() : this("anon");
}Order of Operations
Remember the flow: initializer list and any redirect run first, then the body. Set finals in the list, do extras in the body.
Initializer List vs Body
Use the list for assigning fields, especially finals; use the body for logic like printing, validating beyond assert, or calling methods.
Quick Check
Quick check on initializer lists.
Recap: Init and Redirect
You can now use an initializer list to set finals and compute values, guard with assert, and redirect one constructor to another with this. 🔀
Frequently asked questions
Is the “Initializer Lists and Redirecting” lesson free?
Yes — the full text of “Initializer Lists and Redirecting” 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 “Initializer Lists and Redirecting”?
Set finals and reuse constructors. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Initializer Lists and Redirecting” 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