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);
}


Join thousands of Flutter developers.

Sign up for infrequent updates about Flutter and Dart.

You can get all this content and more in one place. Check out my new book Flutter in Action