RuntimeType<T> class
A non-opaque representation of a Type that can be used at runtime.
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
- Available Extensions
Constructors
- RuntimeType()
-
Create a new RuntimeType representing
T
. - RuntimeType.allowingDynamic()
-
Create a new RuntimeType representing
T
, allowingdynamic
as a valid value forT
.const
Properties
- hashCode → int
-
The hash code for this object.
no setteroverride
- internalType → Type
-
The Type that this RuntimeType represents.
no setter
- isNullable → bool
-
Whether this type is nullable.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
acceptsInstance(
dynamic instance) → bool -
Whether
instance
can be assigned to this type. -
castInstance(
dynamic instance) → T -
Cast
instance
to this type, throwing if the cast fails. -
isEquivalentTo<
U> (RuntimeType< U> other) → bool -
Whether this type is equivalent to
other
. -
isSubtypeOf<
U> (RuntimeType< U> other) → bool -
Whether this is a subtype of
other
. -
isSupertypeOf<
U> (RuntimeType< U> other) → bool -
Whether this is a supertype of
other
. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
nullable(
) → RuntimeType< T?> - Return a nullable version of this RuntimeType.
-
run<
R> (R callback< U>() ) → R -
Run
callback
, passing this type as a type parameter. -
toString(
) → String -
A string representation of this object.
override
-
withInstance<
U, R> (U value, void process(T value), {void otherwise(U value)?}) → void -
Run
process
withvalue
ifvalue
is assignable to this type.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override