title: Create an instance of an object from a String/Symbol without the Class in Dart?
body: <p>I know that it is possible to create an instance from a symbol like is shown in this link:</p>
  
  <p><a href="https://stackoverflow.com/questions/17181108/create-an-instance-of-an-object-from-a-string-in-dart">Create an instance of an object from a String in Dart?</a></p>
  
  <p>But this doesn't work for me since what I want to do is create an instance without having the class.</p>
  
  <p>This problem is caused because I have One class with an internal List:</p>
  
  <pre><code>class MyNestedClass {
    String name;
  }
  
  class MyClass {
    int i, j;
    String greeting;
    List&lt;MyNestedClass&gt; myNestedClassList;
  }
  </code></pre>
  
  <p>And I want to convert a map to this class:</p>
  
  <pre><code>   {
          "greeting": "hello, there",
          "i": 3,
          "j": 5,
          "myNestedClassList": [
             {
               "name": "someName1"
             },{
               "name": "someName2"
             }
          ]
      }
  </code></pre>
  
  <p>right now I am doing something like this:</p>
  
  <pre><code> static void jsonToObject(String jsonString, Object object) {
      Map jsonMap = JSON.decode(jsonString); //Convert the String to a map
      mapToObject(jsonMap, object); //Convert the map to a Object
    }
  
    static void mapToObject(Map jsonMap, Object object) {
      InstanceMirror im = reflect(object); //get the InstanceMirror of the object
      ClassMirror cm = im.type; //get the classMirror of the object
  
      jsonMap.forEach((fieldNameStr, fieldValue) { // For each element in the jsonMap
        var fieldName = new Symbol(fieldNameStr); // convert the fieldName in the Map to String
  
        if (isPrimitive(fieldValue)) { // if fieldValue is primitive (num, string, or bool
                im.setField(fieldName, fieldValue); //set the value of the field using InstanceMirror
        } else if (fieldValue is List) { // else if the fieldValue is a list
            ClassMirror listCm = (cm.declarations[fieldName] as VariableMirror).type; //get the class mirror of the list
            var listReflectee = listCm.newInstance(const Symbol(''), []).reflectee; //create an instance of the field
  
            for(var element in fieldValue) { //for each element in the list
              if(!isPrimitive(element)) { // if the element in the list is a map (i.e not num, string or bool)
                 var listType = listCm.typeArguments[0]; //get the TypeMirror of the list (i.e MyNestedClass from List&lt;MyNestedClass&gt;)
  
                 //This is the line that doesn't work correctly
                 //It should be something like:
                 //
                 //     ClassMirror.fromSymbol(listType.simpleName).newInstance(const Symbol(''), []);
                 //
                 var listObject = (listType as ClassMirror).newInstance(const Symbol(''), []); //create an instance of the specified listType
  
                 mapToObject(element, listObject); //convert the element to Object
              }
              listReflectee.add(element); //add the element to the list
            };
        } else { //else (the field value is a map
          ClassMirror fieldCm = (cm.declarations[fieldName] as VariableMirror).type; // get the field ClassMirror from the parent declarations
          var reflectee = fieldCm.newInstance(const Symbol(''), []).reflectee; //create an instance of the field
          mapToObject(fieldValue, reflectee); // convert the fieldValue, which is a map, to an object
          im.setField(fieldName, reflectee); // set the value of the object previously converted to the corresponding field
        }
      });
    }
  </code></pre>
  
  <p>As you can see the lines that are not actually working are:</p>
  
  <pre><code>var listType = listCm.typeArguments[0]; //get the TypeMirror of the list (i.e MyNestedClass from List&lt;MyNestedClass&gt;)
  var listObject = (listType as ClassMirror).newInstance(const Symbol(''), []); //create an instance of the specified listType
  </code></pre>
  
  <p>since they are creating an instance on <code>localClassMirror</code> and not on <code>MyNestedClass</code>. I'm looking for a method similar to:</p>
  
  <pre><code> ClassMirror.fromSymbol(listType.simpleName).newInstance(const Symbol(''), []);
  </code></pre>
  
  <p>you can see the full source code in the next URL:</p>
  
  <p><a href="https://bitbucket.org/luisvargastijerino/dson/src" rel="nofollow noreferrer">DSON Source Code</a></p>