Volo
Volo

Reputation: 29428

How to perform runtime type checking in Dart?

Dart specification states:

Reified type information reflects the types of objects at runtime and may always be queried by dynamic typechecking constructs (the analogs of instanceOf, casts, typecase etc. in other languages).

Sounds great, but there is no instanceof-like operator. So how do we perform runtime type-checking in Dart? Is it possible at all?

Upvotes: 244

Views: 266649

Answers (11)

YEG
YEG

Reputation: 632

  • .runtimeType property does an exact type match.
  • is keyword matches on parent class types and class extension types
    class Aaa {}

    class Bbb extends Aaa {}
    
    void main() {
      var x = Bbb();
      
      print(x is Aaa);                  // true
      print(x is Bbb);                  // true
      print(x.runtimeType == Aaa);      // false
      print(x.runtimeType == Bbb);      // true
      print(x.runtimeType);             // Bbb
      print(x.runtimeType.runtimeType); // _Type
      print(x.runtimeType.toString());  // Bbb
    }

Upvotes: 3

Hypermona
Hypermona

Reputation: 147

To check the type of a variable use runtimeType

void main() {
  int a = 10;
  print(a.runtimeType);
}

to check whether the type of a variable is the same as your expected use is or runtimeType

void main() {
  int a = 10;
  print(a.runtimeType == int); // true
  //or
  print(a is int); // true
}

Upvotes: 1

Hasan koç
Hasan koç

Reputation: 169

if(value is int ) Returns true if the type of the value is int, else if(value is! int )

Upvotes: 4

Alish Giri
Alish Giri

Reputation: 2238

Simply use .runtimeType on the property like below,

print(unknownDataTypeProperty.runtimeType)

Upvotes: 25

lava
lava

Reputation: 7343

T is The type

   print( T.runtimeType)

enter image description here


enter image description here

Upvotes: 2

Edoardo
Edoardo

Reputation: 5432

Just to clarify a bit the difference between is and runtimeType. As someone said already (and this was tested with Dart V2+) the following code:

class Foo {
  @override
  Type get runtimeType => String;
}
main() {
  var foo = Foo();
  if (foo is Foo) {
    print("it's a foo!");
  }
  print("type is ${foo.runtimeType}");
  
}

will output:

it's a foo! 
type is String

Which is wrong. Now, I can't see the reason why one should do such a thing...

Upvotes: 15

Maxim Saplin
Maxim Saplin

Reputation: 4652

Exact type matching is done via runtimeType property. Checking if an instance or any of its parent types (in the inheritance chain) is of the given type is done via is operator:

class xxx {}

class yyy extends xxx {}

void main() {
  var y = yyy();
  
  print(y is xxx);
  print(y.runtimeType == xxx);
}

Returns:

true
false

Upvotes: 20

sbedulin
sbedulin

Reputation: 4332

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);

Upvotes: 85

Rob
Rob

Reputation: 5286

As others have mentioned, Dart's is operator is the equivalent of Javascript's instanceof operator. However, I haven't found a direct analogue of the typeof operator in Dart.

Thankfully the dart:mirrors reflection API has recently been added to the SDK, and is now available for download in the latest Editor+SDK package. Here's a short demo:

import 'dart:mirrors'; 

getTypeName(dynamic obj) {
  return reflect(obj).type.reflectedType.toString();
}

void main() {
  var val = "\"Dart is dynamically typed (with optional type annotations.)\"";
  if (val is String) {
    print("The value is a String, but I needed "
        "to check with an explicit condition.");
  }
  var typeName = getTypeName(val);
  print("\nThe mirrored type of the value is $typeName.");
}

Upvotes: 22

Patrick
Patrick

Reputation: 4088

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!");
  }
}

Upvotes: 329

Duncan
Duncan

Reputation: 95612

There are two operators for type testing: E is T tests for E an instance of type T while E is! T tests for E not an instance of type T.

Note that E is Object is always true, and null is T is always false unless T===Object.

Upvotes: 21

Related Questions