title: Direct accessing a nested Map of dynamic depth
body: <p>I want to input data in a map at its depth. It is easy to do the following:</p>
  <pre><code>static Map map;
  insert(keyList, x) {
      if(keyList.length==1) map[0] = x;
      if(keyList.length==2) map[keyList[0]][keyList[1]] = x;
      ...
  }
  </code></pre>
  <p>I would, however, like a more intuitive way to do the same (i.e. using a loop over the keyList) which does not rely on hardcoding iterations.</p>
  <p>My attempt at implementing the same led to a resource-heavy recursive implementation:</p>
  <pre><code>Map insertInMap(Map&lt;String, dynamic&gt; m, List&lt;String&gt; keyList, Object o) {
    if (keyList.length == 1) {
      m[keyList.first] = o;
    } else {
      m[keyList.first] = insertInMap(
        Map.from(m[keyList.first]),
        keyList.sublist(1),
        o,
      );
    }
    return m;
  }
  </code></pre>
  <p>I understand the above code is making copies of the Map and refitting them in the original map bottom up. I would like a way to edit the map directly, similar to how map[i][j] works, which I believe does not replicate maps and should thus be much more efficient in memory use as an in-place algorithm.</p>
  <p>My search led me to this as question;
  <a href="https://stackoverflow.com/questions/45200391/accessing-nested-array-at-a-dynamic-depth">Accessing nested array at a dynamic depth</a> which resembles my non-optimal recursive solution. Other answers I attempted searching were about iterating over maps using loops, which are a different topic altogether without the mention of their depth.</p>
  <p>If it is not possible, assistance by way of confirming it would save me time and mental anguish.</p>
  <p>Thanks in advance.</p>