How to perform runtime type checking in Dart? How to perform runtime type checking in Dart? dart dart

How to perform runtime type checking in Dart?


The instanceof-operator is called is in Dart. The spec isn't exactly friendly to a casual reader, so the best description right now seems to be http://www.dartlang.org/articles/optional-types/.

Here's an example:

class Foo { }main() {  var foo = new Foo();  if (foo is Foo) {    print("it's a foo!");  }}


Dart Object type has a runtimeType instance member (source is from dart-sdk v1.14, don't know if it was available earlier)

class Object {  //...  external Type get runtimeType;}

Usage:

Object o = 'foo';assert(o.runtimeType == String);


object.runtimeType returns the type of object

For example:

print("HELLO".runtimeType); //prints Stringvar x=0.0;print(x.runtimeType); //prints double