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: Uncaught TypeError: J.$index$asx(...).call$0 is not a function.
Code examples:
Dart:
context["UpdateNames"] =
(JsObject pTag)
{
print(pTag["function"]("text"));
};
JS:
function execute ()
{
var func = {"function": new Function("str", "return str.length;")};
UpdateNames(func);
}
EDIT:
Solution: Create an object in JavaScript such as this:
this.fun = function (name)
{
var text = "var funs = " + document.getElementById("personalFun").value;
eval(text);
return funs(name);
};
Then create the object in Dart:
caller = new JsObject(context['Point'], []);
Finally call the method to dynamically create the function:
caller.callMethod('fun', [text]);