Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all I think your initial approach is perfectly valid and should not be cast away owing simply to its verbosity. I believe alternative approaches introduce additional complexity and are justified only if you are really in need of dynamic dispatch. (For example if you write library for persistency and you wish to add ability to register arbitrary class for persistency for clients of library) If dynamic dispatch is a must for you I believe there is two main possibility: - Reflection API. Recently reflection library got sync API, so this way is now much more affordable then before. I believe there always will be some cost incurred by reflection anyway. - Use core DART functionality.</p> <p>With the second approach you may use some sort of trick to imitate constructor call dynamically. For instance you may store in map not Type variable but function which returns instance of required class:</p> <p>So your code may look something like</p> <pre class="lang-dart prettyprint-override"><code>static final Map&lt;String, Function&gt; lookup = new Map&lt;String, Function&gt; static void registerClass(String className, factory) { lookup[className] = factory; } static Persistable getInstance(String className, Map map){ return lookup[className](map); } </code></pre> <p>And on client side:</p> <pre class="lang-dart prettyprint-override"><code>.... registerClass('quiz', (map)=&gt; new Quiz.fromMap(map)); registerClass('card', (map)=&gt; new Card.fromMap(map)); </code></pre> <p>(Attention - I did not test this) You may look for working sample code for that approach in <a href="https://github.com/vadimtsushko/objectory" rel="nofollow">https://github.com/vadimtsushko/objectory</a> </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload