DEV Community

francesco agati
francesco agati

Posted on • Edited on

10 Extension Methods examples in Dart: A Comprehensive Guide with Code Examples

Dart extension method are used from string manipulation to mathematical operations on existing classes.

10 Unique Extension Method Examples:

1 - String Extension - capitalizeFirstLetter(): Capitalizes the first letter of a given string.

extension CapString on String {
  String capitalizeFirstLetter() {
    return this[0].toUpperCase() + substring(1);
  }
}
Enter fullscreen mode Exit fullscreen mode

Example usage:

void main() {
  String name = "john doe";
  print(name.capitalizeFirstLetter()); // Output: John Doe
}
Enter fullscreen mode Exit fullscreen mode

2 - List Extension - findMaxElement(): Finds the maximum element in a list of integers.

extension MaxList on List<int> {
  int findMaxElement() {
    return reduce(max);
  }
}
Enter fullscreen mode Exit fullscreen mode

Example usage:

void main() {
  List<int> numbers = [3, 9, 1, 7, 5];
  print(numbers.findMaxElement()); // Output: 9
}
Enter fullscreen mode Exit fullscreen mode

3 - Int Extension - isEven(): Checks if an integer is even.

extension EvenInt on int {
  bool isEven() {
    return this % 2 == 0;
  }
}
Enter fullscreen mode Exit fullscreen mode

Example usage:

void main() {
  int number = 15;
  print(number.isEven()); // Output: false
}
Enter fullscreen mode Exit fullscreen mode

4 - List Extension - reverseList(): Reverses the elements of a list in-place.

extension RevList<T> on List<T> {
  void reverseList() {
    this.asMap().forEach((i, element) => this[i] = this[length - i - 1]);
  }
}
Enter fullscreen mode Exit fullscreen mode

Example usage:

void main() {
  List<String> fruits = ["apple", "banana", "cherry"];
  fruits.reverseList();
  print(fruits); // Output: [cherry, banana, apple]
}
Enter fullscreen mode Exit fullscreen mode

5 - String Extension - isPalindrome(): Checks if a string is a palindrome (reads the same backward as forward).

extension PalinString on String {
  bool isPalindrome() {
    return this == reverse;
  }
}
Enter fullscreen mode Exit fullscreen mode

Example usage:

void main() {
  String word = "radar";
  print(word.isPalindrome()); // Output: true
}
Enter fullscreen mode Exit fullscreen mode

6 - List Extension - average(): Calculates the average of a list of numbers.

extension AvgList on List<num> {
  double average() {
    return reduce((sum, element) => sum + element) / length;
  }
}
Enter fullscreen mode Exit fullscreen mode

Example usage:

void main() {
  List<double> scores = [90.5, 87.3, 92.1, 88.9];
  print(scores.average()); // Output: 89.175
}
Enter fullscreen mode Exit fullscreen mode

7 - Int Extension - factorial(): Calculates the factorial of an integer.

extension FactInt on int {
  int factorial() {
    return (this < 2) ? 1 : this * (this - 1).factorial();
  }
}
Enter fullscreen mode Exit fullscreen mode

Example usage:

void main() {
  int number = 5;
  print(number.factorial()); // Output: 120
}
Enter fullscreen mode Exit fullscreen mode

8 - List Extension - removeDuplicates(): Removes duplicate elements from a list.

extension UniqList<T> on List<T> {
  void removeDuplicates() {
    this.replaceRange(0, length, toSet().toList());
  }
}
Enter fullscreen mode Exit fullscreen mode

Example usage:

void main() {
  List<int> numbers = [1, 2, 3, 4, 4, 5, 6, 6, 7];
  numbers.removeDuplicates();
  print(numbers); // Output: [1, 2, 3, 4, 5, 6, 7]
}
Enter fullscreen mode Exit fullscreen mode

9 - String Extension - countVowels(): Counts the number of vowels in a string.

extension VowelString on String {
  int countVowels() {
    return replaceAll(RegExp(r'[^aeiou]'), '').length;
  }
}
Enter fullscreen mode Exit fullscreen mode

Example usage:

void main() {
  String sentence = "The quick brown fox jumps over the lazy dog";
  print(sentence.countVowels()); // Output: 5
}
Enter fullscreen mode Exit fullscreen mode

10 - List Extension - isSorted(): Checks if a list of integers is sorted in ascending order.

extension SortList on List<int> {
  bool isSorted() {
    return this == toList()..sort();
  }
}
Enter fullscreen mode Exit fullscreen mode

Example usage:

void main() {
  List<int> numbers = [1, 2, 3, 4, 5];
  print(numbers.isSorted()); // Output: true
}
Enter fullscreen mode Exit fullscreen mode

Extension methods in Dart provide a powerful way to add functionality to existing classes without modifying their source code. By using these extensions, you can write cleaner and more maintainable code that is easier to understand and use. In this article, we've presented ten unique examples of extension methods for various data types and demonstrated how they can be used effectively.

Top comments (0)