问题
I am wondering if there is anyway to call a function by its name in dart as in javascript.
I would like to do something as such:
foo["bar"]();
回答1:
I don't want readers to think what the questioner wants isn't possible in Dart, so I'm adding an answer.
You need to use Mirrors to call a method if you have its name available as a string. Here is an example:
import 'dart:mirrors';
class Foo {
bar() => "bar";
}
void main() {
var foo = new Foo();
var mirror = reflect(foo);
print(mirror.invoke(#bar, []).reflectee); // Prints 'bar'.
}
来源:https://stackoverflow.com/questions/19718821/dart-calling-a-member-function-by-function-name