0Pricing
Dart Academy · Lesson

Extensions on Generics

Extend List, Iterable, and type parameters.

Extensions on Generics is a free Dart Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

The Element Type Flows Through

Because you wrote List of T, your method returns the right element type automatically. No casting, no dynamic surprises.

List<int> nums = [1, 2, 3];
int? n = nums.firstOrNull; // typed as int?

Extend Iterable for Reach

Extending Iterable instead of List makes your helper work on lists, sets, and lazy iterables alike. Wider reach, same code.

extension Counts<T> on Iterable<T> {
  int countWhere(bool Function(T) f) => where(f).length;
}

Generic Methods Inside

An extension method can take its own generic parameters too, separate from the type you are extending.

extension Mapping<T> on List<T> {
  List<R> mapTo<R>(R Function(T) f) => map(f).toList();
}

Type Inference Helps You

Dart usually infers the type arguments from the value you call on, so your call sites stay short and clean.

var first = ["a", "b"].firstOrNull; // String?

Extend Map by Two Types

Maps need two parameters. Write Map of K, V and you can build helpers that respect both keys and values.

extension MapPick<K, V> on Map<K, V> {
  V? pick(K key) => this[key];
}

Stay Null-Safe

Returning T? instead of T signals that a value might be missing, keeping callers honest under null safety.

extension SafeLast<T> on List<T> {
  T? get lastOrNull => isEmpty ? null : last;
}

Reuse Across Element Types

One generic extension serves every element type. The same firstOrNull works on numbers, strings, and your own classes.

var who = <User>[].firstOrNull; // User?

Constrain When Needed

If your helper needs specific abilities, add a bound with extends so only suitable element types are allowed.

extension Sums<T extends num> on List<T> {
  num total() => fold(0, (a, b) => a + b);
}

Why Generic Extensions Win

They combine reuse with safety: write a helper once, and it adapts to any element type while preserving the correct return type.

Quick Check

Quick check on keeping extensions type-safe.

Recap: Generic Extensions

You can add a type parameter to an extension, target List, Iterable, or Map, and even bound it with extends to stay reusable and safe. ✅

Frequently asked questions

Is the “Extensions on Generics” lesson free?

Yes — the full text of “Extensions on Generics” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Extensions on Generics”?

Extend List, Iterable, and type parameters. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Extensions on Generics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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