0Pricing
Dart Academy · Lesson

Generic Classes and Methods

Parameterize your own types.

Generic Classes and Methods 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.

Your Own Generics

Built-in types like List are generic, and you can write your own too. A type parameter lets your class adapt to whatever type the caller picks. 🧩

Naming the Type Parameter

You add a type parameter in angle brackets after the class name. The letter T is a common placeholder that stands in for a real type.

class Box<T> {
  T value;
  Box(this.value);
}

Using T Inside

Inside the class, T behaves like any type name. You can use it for fields, parameters, and return values throughout the body.

class Box<T> {
  T value;
  Box(this.value);
  T get() => value;
}

Filling In the Type

When you create the object, you choose the real type. Here T becomes int, so this box only ever holds integers.

var b = Box<int>(42);
int n = b.get();

Let Dart Infer It

Often you can skip the explicit type because Dart reads it from the argument. Inference sees a String and sets T for you.

var b = Box('hi');

Multiple Type Parameters

A class can take more than one type parameter, separated by commas. A pair holding two different types is a classic example.

class Pair<A, B> {
  A first;
  B second;
  Pair(this.first, this.second);
}

Generic Methods

You can also make a single method generic, even in a non-generic class. Put the type parameter right before the parameter list.

T firstOf<T>(List<T> items) => items[0];

Methods Infer Too

A generic method usually infers its type from the arguments you pass. Call it normally and Dart figures out the rest.

var n = firstOf([1, 2, 3]);
var s = firstOf(['a', 'b']);

Type Safety Returns

Because the method tracks T, the result keeps the correct type. firstOf on a String list gives you a String back, no casting needed.

String x = firstOf(['a', 'b']);

Reusable, Not Repetitive

One generic definition replaces many copy-paste versions. You write the logic once and it serves int, String, and any custom type alike.

Pick Clear Names

Single letters like T, E, or K are conventional, but a descriptive name can help. Clarity matters more than brevity in complex code.

Quick Check

Where does a type parameter go on a class?

Recap

You learned to add a type parameter like T to your own classes and methods. Dart fills it in, often by inference, keeping code reusable and safe. ✅

Frequently asked questions

Is the “Generic Classes and Methods” lesson free?

Yes — the full text of “Generic Classes and Methods” 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 “Generic Classes and Methods”?

Parameterize your own types. 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 “Generic Classes and Methods” 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. Why Generics Beat dynamic
  2. Generic Classes and Methods
  3. Bounded Type Parameters With extends
  4. Generic Collections in Practice
← Back to Dart Academy