Skip to content

Type abstract interface

abstract interface class Type

Runtime representation of a type.

Type objects represent types. A type object can be created in several ways:

  • By a type literal, a type name occurring as an expression, like Type type = int;, or a type variable occurring as an expression, like Type type = T;.
  • By reading the run-time type of an object, like Type type = o.runtimeType;.
  • Through dart:mirrors.

A type object is intended as an entry point for using dart:mirrors. The only operations supported are comparing to other type objects for equality, and converting it to a string for debugging.

Properties

hashCode no setter override

int get hashCode

A hash code for the type which is compatible with operator==.

Implementation
dart
int get hashCode;

runtimeType no setter inherited

Type get runtimeType

A representation of the runtime type of the object.

Inherited from Object.

Implementation
dart
external Type get runtimeType;

Methods

noSuchMethod() inherited

dynamic noSuchMethod(Invocation invocation)

Invoked when a nonexistent method or property is accessed.

A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:

dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time error

This invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).

Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.

A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:

dart
class MockList<T> implements List<T> {
  noSuchMethod(Invocation invocation) {
    log(invocation);
    super.noSuchMethod(invocation); // Will throw.
  }
}
void main() {
  MockList().add(42);
}

This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.

If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.

The default behavior is to throw a NoSuchMethodError.

Inherited from Object.

Implementation
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);

toString() override

String toString()

Returns a string which represents the underlying type.

The string is only intended for providing information to a reader while debugging. There is no guaranteed format, the string value returned for a Type instances is entirely implementation dependent.

The string should be consistent, so a Type object for the same type returns the same string throughout a program execution. The string may or may not contain parts corresponding to the source name of declaration of the type, if the type has a source name at all (some types reachable through dart:mirrors may not).

Implementation
dart
String toString();

Operators

operator ==() override

bool operator ==(Object other)

Whether other is a Type instance representing an equivalent type.

The language specification dictates which types are considered to be the equivalent. If two types are equivalent, it's guaranteed that they are subtypes of each other, but there are also types which are subtypes of each other, and which are not equivalent (for example dynamic and void, or FutureOr<Object> and Object).

Implementation
dart
bool operator ==(Object other);