Dart, is it possible to create an instance with the default constructor without mirrors?

Issue

I’m trying to cut down on my dart2js output by utilising @MirrorsUsed, though I currently have to register more types than I would like to as I need it to know that certain classes have a default constructor as I do something like this:

var a = reflectClass(A).newInstance(const Symbol(''), new List<dynamic>()).reflectee;

but if I haven’t registered class A with @MirrorsUsed it fails to create an instance of A because it doesn’t know whether it has a default constructor given by const Symbol('').

Is there anyway in dart for creating an instance of a Type just from the Type without having to reflect on it asI have done above?

Solution

If you know the types in advance you could create a factory and avoid mirrors entirely. This way you have to hardcode the instantiation though but might be less maintenance work as applying MirrorsUsed.

Answered By – Günter Zöchbauer

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *