Reputation: 20255
in Dart
, you can assign a class
to a variable, eg,
var klass = String;
but once assigned, how do you used it?
var str = new klass(); ===> ERROR: malformed type used
once one has a class
in a variable, how does one use it to instantiate
a new object
of that class
?
Upvotes: 2
Views: 69
Reputation: 76373
You have to use dart:mirrors.
import 'dart:mirrors';
class A {
A();
A.named(String value);
}
main() {
final t = A;
// same as 'new A()'
final a1 = reflectClass(t).newInstance(const Symbol(''), []).reflectee;
// same as 'new A.named('test')'
final a2 = reflectClass(t).newInstance(#named, ['test']).reflectee;
}
Upvotes: 3