0PricingLogin
Dart Academy · Lesson

Extensions on Generics

Extend List, Iterable, and type parameters.

Extending Collections

The real magic appears when you extend generic types like List or Iterable. One extension can help every element type at once.

Declare a Type Parameter

Put a type parameter after the extension name, then use it in the on clause. Now your helper stays fully type-safe.

extension Firsts<T> on List<T> {
  T? get firstOrNull => isEmpty ? null : this[0];
}

All lessons in this course

  1. Writing Your First Extension
  2. Extensions on Generics
  3. Static Extension Members and Conflicts
  4. Extension Types for Zero-Cost Wrappers
← Back to Dart Academy