Extension methods
on Sunday, 19th of July, 2020
Extension methods are new as of Dart 2.7. They allow you to add functionality to existing libraries and classes. For example, you can add extra functionality to the Dart core String library, that are only available in your app.
extension on DateTime {
String get humanize {
// you have access to the instance in extension methods via 'this' keyword.
return "${this.day}/${this.month}/${this.year}";
}
}
void main() {
final dateTime = DateTime.now();
print(dateTime.humanize);
}- previous: Methods: static, private, etc