Ad space (banner)
Lesson 19 / 20

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.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช 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).

Ad space (banner)
Ad space (in-article)