📱 Flutter Intermediate

What is an extension in Dart?

Answer

Extensions allow you to add new methods to existing types without modifying their source code or using inheritance. Declare an extension with extension StringUtils on String { ... } and then call its methods on any String. For example: extension on DateTime { bool get isToday => day == DateTime.now().day; }. Extensions are resolved statically at compile time and do not actually modify the original class. They are widely used to add utility helpers to built-in types like String, List, and DateTime.