Skip to content

override_changes_default_value

Reports an optional parameter of an override whose default differs from the overridden member’s. Use the same default.

Tier
advisory
Scope
Dart
Lowest preset
recommended
Fix
none

An optional parameter of an overriding method whose default differs from the default of the member it overrides. The violation covers the parameter.

  • A named parameter is matched by its name, an optional positional one by its place.
  • Defaults are compared as constant values, so two spellings of one value are the same default. Where one cannot be evaluated, nothing is reported.
  • An override that writes no default has the default null, which differs from a default the base gives. A base that writes no default leaves the override free to choose.
  • Every member the method overrides is compared: of superclasses, mixins and interfaces alike.
  • A parameter the override’s body never reads is not reported.

A missing argument gets the default of the method that runs, whatever type the call is written against. So a call through the base type gets a value its signature does not show: code written against render(text, width: 80) runs with the override’s 40. Changing the static type of a variable changes nothing, which makes the difference hard to see.

Violating
abstract class Renderer {
String render(String text, {int width = 80});
}
class Narrow implements Renderer {
@override
String render(String text, {int width = 40}) => text.padLeft(width);
}
Corrected
abstract class Renderer {
String render(String text, {int width = 80});
}
class Narrow implements Renderer {
@override
String render(String text, {int width = 80}) => text.padLeft(width);
}

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// This renderer is narrow on purpose; callers pass a width.
// ignore: override_changes_default_value
String render(String text, {int width = 40}) => text;

// ignore_for_file: assay/override_changes_default_value silences it in a whole file, and the prefixed // ignore: assay/override_changes_default_value works too. See Suppressions.