36

How can I determine whether an object is of a class or not in the Dart language?

I'm looking to do something like the following:

if (someObject.class.toString() == "Num") {
    ...
}

And what is the returned value type? Will it have to be a String?


The mirror library has been up and down and seems to be subject to rapid change right now, as the one thing I did find simply did not work as shown.

Günter Zöchbauer
  • 490,478
  • 163
  • 1,733
  • 1,404
george koller
  • 2,363
  • 3
  • 17
  • 26

3 Answers3

53

Recently Object got 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 now skip toString operation and directly compare runtimeType of object with target type as in

myObject.runtimeType == int

or

myObject.runtimeType == Animal
nbro
  • 12,226
  • 19
  • 85
  • 163
Vadim Tsushko
  • 1,448
  • 9
  • 10
  • 1
    One can compare directly using for instance `myObject.runtimeType == int`. – Daniel Nov 13 '14 at 11:22
  • 1
    Any advantages compared to `myObject is int`? `is` also returns true even when it only implements the other type this might not be the desired results in some cases. It was mentioned occasionally that `runtimeType` is not reliable because it can be overridden and should only be used for debugging purposes. – Günter Zöchbauer Nov 14 '14 at 07:55
  • 1
    I believe equality with runtimeType give you more dynamic capabilities. For example see that function: `testType(object,type) => object.runtimeType == type`. You can use it as `testType(myObject,int)` I believe it is impossible with `is` operator. But I should check it, maybe I wrong – Vadim Tsushko Nov 14 '14 at 09:31
  • 2
    I've checked, my guess was right. You cannot pass `type literal` as parameter of function and then use it in `is` operation. So `isOfType(obj, Type type) => obj is type` is incorrect but `isOfType(obj, Type type) => obj.runtimeType == type` works well – Vadim Tsushko Nov 15 '14 at 06:39
  • Better answer yet – Vidor Vistrom Nov 16 '18 at 16:38
  • 2
    If you use dart for web, minified to js code will return minified value! – Zufar Muhamadeev Dec 18 '18 at 07:43
43
  • By using the is and is! operators, like this:

    if (someObject is T)
    

    From the documentation:

    The is and is! operators are handy for checking types. The result of obj is T is true if obj implements the interface specified by T. For example, obj is Object is always true.

  • Using the Mirrors API (see this example):

    Expect.equals('T', someObject.simpleName)
    
illright
  • 3,586
  • 2
  • 24
  • 43
Eliran Malka
  • 14,498
  • 5
  • 72
  • 96
  • 1
    Thanks! Works beautifully. See quick test below: .... bool noteIsString(var note) { return( note is String ); } bool test0 = noteIsString("string"); bool test1 = noteIsString(123.45); print("test0 result is: $test0"); //=> true print("test1 result is: $test1"); //=> false – george koller Oct 14 '12 at 07:21
0

Here is a simple explanation with a solution.

You have:

Object obj =t1;
where t1 is an object of class T.

And you have other object T called t.

T t = new T();

How to check if obj is the same type of t ?

Solution:

if(obj is t)
     print('obj is typeof t')
   else print('obj is not typeof t') 
David Buck
  • 3,439
  • 29
  • 24
  • 31