I have a instance of Type
, but I want its fully qualified name. How can I do this? I know I have to use Mirrors (Dart's reflection library).
Asked
Active
Viewed 1,748 times
9

Seth Ladd
- 112,095
- 66
- 196
- 279
1 Answers
9
Use the new reflectClass
top-level function from dart:mirrors
.
Here's an example:
import 'dart:html';
import 'dart:mirrors';
class Awesome {
// ...
}
void main() {
var awesome = new Awesome();
Type type = awesome.runtimeType;
ClassMirror mirror = reflectClass(type);
Symbol symbol = mirror.qualifiedName;
String qualifiedName = MirrorSystem.getName(symbol);
query('#name').text = qualifiedName;
}
The qualifiedName
should be something like:
http://127.0.0.1:3030/Users/sethladd/dart/type_name/web/type_name.dart.Awesome
Note, this works in build 21753 or higher. Also, this doesn't currently work in dart2js yet. We plan to support it in dart2js.

Juniper Belmont
- 3,296
- 2
- 18
- 28

Seth Ladd
- 112,095
- 66
- 196
- 279
-
Great and with extension methods it would be even better, i.e. you could have: `String qualifiedName = reflectClass(type).qualifiedName.getName()` - [let Gilad know!](https://code.google.com/p/dart/issues/detail?id=13) :) – mythz Apr 19 '13 at 18:43
-
What's the status of using a type directly like `reflectClass(Awesome)`? – Kai Sellgren Apr 21 '13 at 11:25
-
@KaiSellgren: The implementation of reflectClass does not care whether you have gotten the Type object through obj.runtimeType or by using the type literal. Put differently, the status of reflectClass(Awesome) should be the same as the status of reflectClass((new Awesome()).runtimeType). If you experience this not being the case, please file a bug at http://dartbug.com . – Ivan Posva Apr 26 '13 at 12:31