checkPrivateFieldHasBothGetterAndSetter static method

bool checkPrivateFieldHasBothGetterAndSetter(
  1. FieldElement field,
  2. ClassElement aClass
)

Checks if a private field has both a getter and setter in the class.

Implementation

static bool checkPrivateFieldHasBothGetterAndSetter(FieldElement field, ClassElement aClass,) {
  if (!field.name.startsWith('_')) {
    return true;
  }

  final publicName = field.name.substring(1);

  bool hasGetter = false;
  bool hasSetter = false;

  for (final accessor in aClass.accessors) {
    if (accessor.isSynthetic) continue;

    final accessorName =
    accessor.displayName.replaceAll('=', '');

    if (accessorName != publicName) continue;

    if (accessor.isGetter) {
      hasGetter = true;
    }

    if (accessor.isSetter) {
      hasSetter = true;
    }
  }

  return hasGetter && hasSetter;
}