D
Dart LanguageEasy20 XP3 min read

What are Dart extension methods and when should you use them?

TL;DR: Extension methods add new methods to existing types without subclassing or wrapping. They're resolved statically at compile time and don't affect the runtime type.

Full Answer

Extensions let you add methods to any type: String, int, List<T>, or even Widget. They appear as methods on the type in the IDE but are just static method calls under the hood.

🎯

Flutter community extensions like extension on Widget { Widget padding(...) } make widget trees much more readable. The on BuildContext pattern enables context.go(route) syntax (go_router).

Code Examples

dartExtension on String and Widget
Output
true
Hello

Interview Tip

💡

Extensions are resolved statically — if you have two extensions providing the same method name on the same type, you must use explicit extension syntax: MyExtension(obj).method(). They work great for DSLs.

#extensions#extension-methods#syntax-sugar