How do I get an actual object dynamically in Dart?

76 views Asked by At

Tl; dr: I get the class symbol and arguments dynamically, how do I get the object and not some mirror thingy.

Hi,

I have a situation where I get the symbol (or string) of a class name and the arguments (maybe positional, maybe optional positional, or named). Can I do the following?

class SomeClass{
    SomeClass(String a, {int b: 3}) { /* someCode */ }
    // someContent
}

List someList = ["SomeClass", 'a', {"b": 5}];

SomeClass objectFromList = /* someFancyMagicHappensHere */;

Where someList comes from is of no relevance, it may be a from a simple List, from an XML or from outer space. I have some influence of the format of someList, so this may be changed to a map, contain symbols etc. I know i could write a factory for a number of cases but I am looking for a more general solution.

Is there someFancyMagic in Dart?

Cheers and Thanks

P.S.: If there is no fancyMagic, is there a specific reason? Is this regarded an antipattern? Is there a better approach? Is the language simply not made for it (yet)?

0

There are 0 answers