RuntimeType<T>.allowingDynamic constructor

const RuntimeType<T>.allowingDynamic()

Create a new RuntimeType representing T, allowing dynamic as a valid value for T.

Only use this constructor if you want a constant RuntimeType. If you want a RuntimeType representing dynamic, consider using dynamicType instead.

Normally, Type objects are opaque and cannot be used for actual logic. RuntimeType allows for operations such as sub- or super-type checking, type checks, and casting using a variable instead of a literal.

To use, replace your type literals with calls to the RuntimeType constructor:

// Before
final stringType = String;

// After
final stringType = RuntimeType<String>();

Though it adds support for some operations, RuntimeType is not a replacement for type literals. For example, it cannot be used as a type argument as they are required to be type literals. Additionally, RuntimeType can be used at runtime to perform operations on types but it cannot extract type information at runtime:

final something = 'foo' as dynamic;

RuntimeType<T> extractType<T>(T instance) => RuntimeType<T>();

final typeOfSomething = extractType(something); // Still dynamic

Implementation

const RuntimeType.allowingDynamic();