noSuchMethod method
- Invocationinvocation
Invoked when a nonexistent method or property is accessed.
A dynamic member invocation can attempt to call a member whichdoesn't exist on the receiving object. Example:
dynamic object = 1;object.add(42); // Statically allowed, run-time errorThis invalid code will invoke thenoSuchMethod methodof the integer1 with anInvocation representing the.add(42) call and arguments (which then throws).
Classes can overridenoSuchMethod to provide custom behaviorfor such invalid dynamic invocations.
A class with a non-defaultnoSuchMethod invocation can alsoomit implementations for members of its interface.Example:
class MockList<T> implements List<T> { noSuchMethod(Invocation invocation) { log(invocation); super.noSuchMethod(invocation); // Will throw. }}void main() { MockList().add(42);}This code has no compile-time warnings or errors even thoughtheMockList class has no concrete implementation ofany of theList interface methods.Calls toList methods are forwarded tonoSuchMethod,so this code willlog an invocation similar toInvocation.method(#add, [42]) and then throw.
If a value is returned fromnoSuchMethod,it becomes the result of the original invocation.If the value is not of a type that can be returned by the originalinvocation, a type error occurs at the invocation.
The default behavior is to throw aNoSuchMethodError.
Implementation
@pragma("vm:entry-point")@pragma("wasm:entry-point")external dynamic noSuchMethod(Invocation invocation);