invokeMethod static method

dynamic invokeMethod(
  1. dynamic obj,
  2. String name,
  3. List args
)

Invokes an object method by its name with specified parameters.

  • obj an object to invoke.
  • name a name of the method to invoke.
  • args a list of method arguments. Returns the result of the method invocation or null if method returns void.

Implementation

static dynamic invokeMethod(obj, String name, List args) {
  if (obj == null) throw Exception('Object cannot be null');
  // if (name == null) throw Exception('Method name cannot be null');

  var foundName = _findMethod(obj, name);
  if (foundName != null) {
    try {
      var im = reflect(obj);
      return im.invoke(foundName, args).reflectee;
    } catch (ex) {
      // Ignore exceptions
    }
  }

  return null;
}