You can do that with mirrors API :
import 'dart:mirrors';
class Test {
method1() => "hello";
}
main() {
print(existsFunction("main")); // true
print(existsFunction("main1")); // false
print(existsMethodOnObject(new Test(), "method1")); // true
print(existsMethodOnObject(new Test(), "method2")); // false
}
bool existsFunction(String functionName) => currentMirrorSystem().isolate
.rootLibrary.functions.containsKey(functionName);
bool existsMethodOnObject(Object o, String method) => reflect(o).type.methods
.containsKey(method);
existsFunction
only tests if a function with functionName
exists in the current library. Thus with functions available by import
statement existsFunction
will return false
.