- 01Setting up (what you'll need)
- 02Your first output (print)
- 03Variables and types (var and annotations)
- 04Null safety (? and !)
- 05Branching (the if statement)
- 06Repeating (the for loop)
- 07Writing your own function
- 08Repeating (the while loop)
- 09Working with lists (List)
- 10Working with maps (Map)
- 11Classes (object orientation)
- 12Constructors
- 13Inheritance
- 14Named arguments
- 15Asynchronous work (Future, async, await)
- 16Working with strings
- 17The switch statement
- 18The ternary operator
- 19Extension methods
- 20[Project] Build a small inventory system
Extension methods
This lesson covers extension methods, which add new methods to an existing type.
Writing extension Name on Type { ... } lets you add your own methods to an existing type such as String or int after the fact.
The sample code uses extension NumberFormatting on int to add toCommaString(), which converts an integer into a comma-separated string.
A common early stumble is picturing why you would want this. It helps to think of it as "a way to add functionality from outside when you cannot modify the existing class directly".
In professional work, extension methods are often used to attach project-specific conveniences to a type when the Dart standard library has no equivalent.
๐งช This site can't compile or run Dart directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
