Using generic methods

Initially, Dart’s generic support was limited to classes.A newer syntax, called generic methods, allows type arguments on methods and functions:

  1. T first<T>(List<T> ts) {
  2. // Do some initial work or error checking, then...
  3. T tmp = ts[0];
  4. // Do some additional checking or processing...
  5. return tmp;
  6. }

Here the generic type parameter on first (<T>)allows you to use the type argument T in several places:

  • In the function’s return type (T).
  • In the type of an argument (List<T>).
  • In the type of a local variable (T tmp).

For more information about generics, seeUsing Generic Methods.