getMethodMirror function Null safety

Symbol getMethodMirror(
  1. dynamic instanceMirror,
  2. String methodName

Find and return the method in the class of the mirror of the instance. Throw MethowNorFound if the methodName is private or an attribute or not found,


Symbol getMethodMirror(dynamic instanceMirror, String methodName) {
  ClassMirror classMirror = instanceMirror.type;
  for (var classMember in classMirror.declarations.keys) {
    var instanceMethod = MirrorSystem.getName(classMember);
    if (instanceMethod == methodName) {
      var methodMirror = classMirror.declarations[classMember];
      if (methodMirror is! MethodMirror || methodMirror.isPrivate) {
        throw MethodNotFoundException();
      return classMember;
  throw MethodNotFoundException();