Freewind
Freewind

Reputation: 198368

How to determine if two instances have the same type, in Dart?

Let's say I get two instances in my code and I don't know their types. How to check it?

If in Java, I can use this code:

a.getClass() == b.getClass()

But in Dart, I can't find similar methods. Although there is the dart:mirrors providing reflect(instance) function, which may let me do it, but I'm not sure if that's a correct solution since it looks complicated.

Upvotes: 5

Views: 625

Answers (3)

Saïd Tahali
Saïd Tahali

Reputation: 199

if you want to compare a and b you can use

if(a.runtimeType == b.runtimeType);

but if you want to confirm that a is the type you want you need to do this

if(a.runtimeType.toString()=="DivElement");//a is a div for instance

because runtimeType's value is not a string

Upvotes: 0

Semih Eker
Semih Eker

Reputation: 2409

I think dart:mirrors (reflection) API helps you. Look at this page :

http://blog.dartwatch.com/2012/06/dartmirrors-reflection-api-is-on-way.html

Also you can look this question(with runtime solution)

How do I get the qualified name from a Type instance, in Dart?

Upvotes: 1

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657957

a.runtimeType == b.runtimeType

Upvotes: 5

Related Questions