Anonymous Functions and Closures
Capture variables in inline functions.
Anonymous Functions and Closures 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.
Functions Without Names
Sometimes you need a function once and never again. An anonymous function has no name and is written right where you use it. ✨
The Basic Shape
An anonymous function is just parameters and a body, with no name in front. You can store it in a variable.
var greet = (String n) {
print('Hi ' + n);
};Calling the Stored Function
Once stored, you call it through the variable just like any named function, using parentheses.
greet('Ada');Passing One Inline
Most often you pass an anonymous function straight into a method. Here forEach calls it for every item.
[1, 2, 3].forEach((n) {
print(n * 2);
});Arrow Anonymous Functions
For a one-expression body, combine anonymous functions with the arrow to keep things compact.
var doubled = [1, 2, 3].map((n) => n * 2);What Is a Closure?
A closure is a function that remembers variables from where it was created, even after that outer scope ends.
Capturing a Variable
The inner function below captures step. It keeps using that value whenever you call the returned function later.
Function adder(int step) {
return (int x) => x + step;
}Each Closure Is Its Own
Call the factory twice and each returned closure keeps a separate captured value. They do not interfere.
var add5 = adder(5);
var add10 = adder(10);Closures Hold State
A captured variable can change between calls, letting a closure remember progress like a tiny counter.
int count = 0;
var tick = () => ++count;Why It Matters
Callbacks, event handlers, and list methods all rely on closures. They let you bundle behavior with the data it needs.
Keep Them Small
Inline closures shine when short. If one grows complex, give it a real name so others can read it easily.
Quick Check
What makes a function a closure?
Recap
You met anonymous functions for one-off use and closures that capture surrounding variables to carry their own state. 🎯
Frequently asked questions
Is the “Anonymous Functions and Closures” lesson free?
Yes — the full text of “Anonymous Functions and Closures” 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 “Anonymous Functions and Closures”?
Capture variables in inline functions. 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 “Anonymous Functions and Closures” 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
- Arrow Syntax for One-Line Functions
- Anonymous Functions and Closures
- Nullable Types and the Question Mark
- Null-Aware Operators: ?., ??, and !