How to tell if an object is an instance of a class
By using the
is
andis!
operators, like this:if (someObject is T)
From the documentation:
The
is
andis!
operators are handy for checking types. The result ofobj is T
is true ifobj
implements the interface specified byT
. For example,obj is Object
is always true.Using the Mirrors API (see this example):
Expect.equals('T', someObject.simpleName)
Recently Object
got the runtimeType
getter. So now, we may not only compare type of object with another type, but actually get the class name of an object.
As in:
myObject.runtimeType.toString()
Furthermore, in the current version of Dart, you can skip the toString
operation and directly compare runtimeType
of object with target type:
myObject.runtimeType == int
or
myObject.runtimeType == Animal