title: Dart: Create method from string
body: <p>I've been trying to create methods dynamically from strings using Dart to no avail. String example: "(String str) => return str.length;". The idea is to allow users to create their own functions to apply to a given string. The only thing I've found is NoSuchMethod which does not seem to apply to my case. I tried using new Function in JavaScript but when passing the function to Dart and executing it, I get the following error: <strong><em>Uncaught TypeError: J.$index$asx(...).call$0 is not a function</em></strong>.</p>
  
  <p>Code examples:</p>
  
  <p>Dart:</p>
  
  <pre><code>context["UpdateNames"] =
  
  (JsObject pTag)
  {
      print(pTag["function"]("text"));
  };
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>function execute ()
  {
      var func = {"function": new Function("str", "return str.length;")};
      UpdateNames(func);
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>Solution: Create an object in JavaScript such as this:</p>
  
  <pre><code>this.fun = function (name)
    {
      var text = "var funs = " + document.getElementById("personalFun").value;
      eval(text);
      return funs(name);
    };
  </code></pre>
  
  <p>Then create the object in Dart:</p>
  
  <pre><code>caller = new JsObject(context['Point'], []);
  </code></pre>
  
  <p>Finally call the method to dynamically create the function:</p>
  
  <pre><code>caller.callMethod('fun', [text]);
  </code></pre>