Richard
Richard

Reputation: 183

Dart: how to get the "Type" from the class

How do I get the "Type" instance of a given class?

Based on http://www.dartlang.org/articles/m1-language-changes/#first-class-types, for example a construct like:

class Foo{}

Type type = Foo;

seems to pass the analyzer ok, but gives an error when run in Dartium. Is this just not supported in the VM yet, or is the syntax wrong? Or do I need to annotate Foo somehow, so that type info is retained at runtime, or something?

thx

Richard.

Upvotes: 2

Views: 2775

Answers (2)

Stim
Stim

Reputation: 1465

Now you can with https://api.dartlang.org/stable/2.3.1/dart-core/Object/runtimeType.html.

class Foo{}
var foo = Foo();

assert(foo.runtimeType == Foo);

Upvotes: 1

Richard
Richard

Reputation: 183

Based on https://groups.google.com/a/dartlang.org/forum/#!msg/misc/P3XdXeoZ4CY/9IBwkV-CTy0J I guess this feature is not in the dart vm as yet!

Upvotes: 1

Related Questions