noSuchMethod method

  1. @override
dynamic noSuchMethod(
  1. Invocation mirror
)
override

noSuchMethod() If we try to access a property using dot notation (eg: o.wibble ), then noSuchMethod will be invoked, and identify the getter or setter name. It then looks up in the map contained in _objectData (represented using this (as this class implements Map, and forwards it's calls to that class. If it finds the getter or setter then it either updates the value, or replaces the value.

If isImmutable = true, then it will disallow the property access even if the property doesn't yet exist.

Implementation

@override
dynamic noSuchMethod(Invocation mirror) {
  int positionalArgs = 0;
  positionalArgs = mirror.positionalArguments.length;
  String property = 'Not Found';

  if (mirror.isGetter && (positionalArgs == 0)) {
    // Synthetic getter
    property = _symbolToString(mirror.memberName);
    if (containsKey(property)) {
      return this[property];
    }
  } else if (mirror.isSetter && positionalArgs == 1) {
    // Synthetic setter
    // If the property doesn't exist, it will only be added
    // if isImmutable = false
    property = _symbolToString(mirror.memberName, true);
    if (!isImmutable!) {
      this[property] = mirror.positionalArguments[0];
    }
    return this[property];
  }

  // If we get here, then we've not found it - throw.
  _log('noSuchMethod:: Not found: $property');
  _log('noSuchMethod:: IsGetter: ${mirror.isGetter}');
  _log('noSuchMethod:: IsSetter: ${mirror.isGetter}');
  _log('noSuchMethod:: isAccessor: ${mirror.isAccessor}');
  return super.noSuchMethod(mirror);
}