match_getter_setter_field_names
This rule flags a getter and setter pair that does not read and write the same backing field.
This is the copy-paste bug that type checking cannot catch: both members compile, both have the right signature, and the mismatch only shows up as a value that will not stick. It is most common where a class has several similar pairs, which is exactly where it is hardest to spot by reading.
Only a pair whose bodies are a single field reference is compared. A getter that computes, or a setter that validates before assigning, has no single field to compare and is skipped rather than guessed at. A compound assignment (+=, ??=) reads the field too, so its asymmetry can be deliberate and is also skipped.
This rule is in the recommended preset.
class Box { int _width = 0; int _height = 0;
int get width => _width; set width(int value) => _height = value; // writes the wrong field}class Box { int _width = 0; int _height = 0;
int get width => _width; set width(int value) => _width = value;}Configuration
Section titled “Configuration”This rule is on with preset: recommended or preset: opinionated.
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: match_getter_setter_field_names: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_getter_over_method— Make a no-argument value read a getter.avoid_accessing_other_classes_private_members— Make the underscore mean what everyone reads it as.avoid_commented_out_code— Detect and flag commented-out code.avoid_complex_conditions— Keep boolean conditions within an operand budget.