ParameterizedTypeReference<T> constructor

ParameterizedTypeReference<T>()

Creates a type reference that captures the generic type parameter.

The factory constructor returns an anonymous subclass that preserves the generic type information at runtime.

Example:

// Correct - creates anonymous subclass
final goodRef = ParameterizedTypeReference<Map<String, dynamic>>();

// Incorrect - loses type information
final badRef = _ParameterizedTypeReference<Map<String, dynamic>>();

A runtime type reference for capturing and working with parameterized/generic types.

This class solves the Dart limitation where List<String>.runtimeType loses generic type arguments. By creating a subclass (typically anonymous), the type information is preserved at runtime.

Usage

Basic Usage

final listRef = ParameterizedTypeReference<List<String>>();
print(listRef.getType()); // List<String>

With Complex Types

final complexRef = ParameterizedTypeReference<Map<String, List<int>>>();
print(complexRef.getType()); // Map<String, List<int>>

With ResolvableType

final ref = ParameterizedTypeReference<Set<double>>();
final resolvable = ref.getResolvableType();
print(resolvable.genericParameters[0].type); // double

Implementation Notes

  • Always create as an anonymous subclass using the factory constructor
  • The actual type capture happens through Dart's type inference
  • Works with nested generic types (e.g., Map<String, List<int>>)

Implementation

factory ParameterizedTypeReference() => _ParameterizedTypeReference<T>();