Writing Your First Extension
Bolt new methods onto a built-in type.
Writing Your First Extension is a free Dart Academy lesson on CoddyKit — lesson 1 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.
Methods You Cannot Add
Ever wished int had a handy method it just does not have? You cannot edit the built-in class, but Dart gives you another way in.
Meet the extension
An extension lets you bolt new methods onto an existing type without subclassing or touching its source. 🎁
extension on String {
// new methods go here
}The Basic Shape
You write extension Name on Type, then add methods inside the braces. The name is yours; the type is what you are extending.
extension NumberParsing on String {
int doubled() => int.parse(this) * 2;
}this Means the Receiver
Inside an extension, this refers to the value you called the method on, exactly like inside a normal class method.
extension on int {
int squared() => this * this;
}Call It Like a Real Method
Once defined, your method feels totally native. You call it with a dot, just like methods that ship with the type.
print(5.squared()); // 25Add a Getter, Not a Method
Extensions can add getters too. Drop the parentheses and you get a clean, property-like read on any value.
extension on int {
bool get isEven2 => this % 2 == 0;
}
print(4.isEven2);Extensions Are Static
Resolution happens at compile time based on the static type. No runtime change is made to the original class at all.
Naming Is Optional
You may even write an unnamed extension. It still works, but a name helps you hide or import it later when needed.
extension on double {
String money() => "$" + toStringAsFixed(2);
}Keep One Idea Per Extension
Group related helpers under a focused name. Small, well-named extensions read better than one giant grab-bag.
extension StringCasing on String {
String shout() => toUpperCase() + "!";
}You Cannot Add Fields
Extensions add methods and getters, but not new instance fields. There is nowhere on the original object to store them.
Why This Is Powerful
Extensions give you readable, discoverable helpers without wrapper classes. Your everyday types gain exactly the vocabulary your app needs.
Quick Check
Let us check the core syntax of an extension.
Recap: Your First Extension
You learned that an extension on Type adds methods and getters to existing types, uses this for the receiver, and resolves statically. 🚀
Frequently asked questions
Is the “Writing Your First Extension” lesson free?
Yes — the full text of “Writing Your First Extension” 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 “Writing Your First Extension”?
Bolt new methods onto a built-in type. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Writing Your First Extension” 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
- Writing Your First Extension
- Extensions on Generics
- Static Extension Members and Conflicts
- Extension Types for Zero-Cost Wrappers