invokeGetter method Null safety

Object? invokeGetter(
  1. String getterName
)

Invokes a getter and returns the result. The getter can be the implicit getter for a field, or a user-defined getter method.

Let o be the object reflected by this mirror, let f be the simple name of the getter denoted by fieldName, Then this method will perform the getter invocation o.f in a scope that has access to the private members of o (if o is a class or library) or the private members of the class of o (otherwise).

If this mirror is an InstanceMirror, and fieldName denotes an instance method on its reflectee, the result of the invocation is a closure corresponding to that method.

If this mirror is a LibraryMirror, and fieldName denotes a top-level method in the corresponding library, the result of the invocation is a closure corresponding to that method.

If this mirror is a ClassMirror, and fieldName denotes a static method in the corresponding class, the result of the invocation is a closure corresponding to that method.

If the invocation returns a result r, this method returns the result r. If the invocation causes a compilation error the effect is the same as if a non-reflective compilation error had been encountered. If the invocation throws an exception e (that it does not catch) this method throws e.

Note that the return type of the corresponding method in dart:mirrors is InstanceMirror.

Required capabilities: invokeGetter on an instance mirror requires a matching InstanceInvokeCapability or InstanceInvokeMetaCapability, and it may target a getter or a method (in which case it is a tear-off operation); similarly, invokeGetter on a class mirror requires a matching StaticInvokeCapability or StaticInvokeMetaCapability, and invokeGetter on a top-level function requires a matching TopLevelInvokeCapability or TopLevelInvokeMetaCapability.

Implementation

//
// TODO(eernst) doc: make this comment more user friendly.
// TODO(eernst) doc: revise language on private members when semantics known.
///
/// Required capabilities: [invokeGetter] on an instance mirror requires a
/// matching [InstanceInvokeCapability] or [InstanceInvokeMetaCapability],
/// and it may target a getter or a method (in which case it is a tear-off
/// operation); similarly, [invokeGetter] on a class mirror requires a
/// matching [StaticInvokeCapability] or [StaticInvokeMetaCapability], and
/// [invokeGetter] on a top-level function requires a matching
/// [TopLevelInvokeCapability] or [TopLevelInvokeMetaCapability].
Object? invokeGetter(String getterName);