use-setters-to-change-properties
Same as use_setters_to_change_properties in Dart lints
- Group
- Style
- Path
lint/style/use-setters-to-change-properties- Default
- Off, opt in
- Origin
- Dart lints
- Rule
- 503 of 512
Flags one-parameter void methods that only assign that parameter to a
field.
A method whose whole body is field = value conceptually changes a property;
a setter states that directly and lets callers use assignment syntax.
class Rectangle {
num width = 0;
void setWidth(num width) { // flagged
this.width = width;
}
set height(num value) => _height = value; // preferred
}
The assignment target must resolve to a field or setter, including a
property on another typed receiver. Overriding methods are exempt;
extension methods never override, even with @override.
Extension receivers use their declared type-parameter bounds; null assertions
and null-aware assignments look up properties on the non-null receiver.
Unavailable external inheritance metadata conservatively exempts public
methods; private names cannot override across libraries.
Invalid
class Rectangle {
num width = 0;
num _height = 0;
void setWidth(num width) {
this.width = width;
}
void changeHeight(num value) => _height = value;
void updateWidth(num value) {
width = value;
}
num get height => _height;
}
class Base {
int count = 0;
}
// Inherited field assigned through `this`.
class Derived extends Base {
void setCount(int value) => this.count = value;
}
mixin Named {
String name = '';
void rename(String newName) => name = newName;
}
// Assigning through an explicit setter.
class Box {
int _size = 0;
set size(int value) => _size = value;
int get size => _size;
void resize(int value) {
size = value;
}
}
class Remote extends SomeUnknownBase {
int port = 0;
void setPort(int value) => port = value;
}
class PropertyBase {
int port = 0;
}
class InheritedProperty extends PropertyBase {
void setPort(int value) => port = value;
}
class Payload {
int value = 0;
}
class WritesOther {
final Payload payload = Payload();
void update(int value) => payload.value = value;
void updateArgument(Payload other) => other.value = other.value;
}
// Resolve the implicit property target through a locally declared extension.
class ExtensionTarget {
int value = 0;
}
extension ChangeValue on ExtensionTarget {
void changeValue(int newValue) {
value = newValue;
}
void changeExplicitly(int newValue) => this.value = newValue;
} void setWidth(num width) { void changeHeight(num value) => _height = value; void updateWidth(num value) {class Derived extends Base { void setCount(int value) => this.count = value; void rename(String newName) => name = newName; void resize(int value) { int port = 0; void setPort(int value) => port = value;class InheritedProperty extends PropertyBase { void setPort(int value) => port = value; final Payload payload = Payload(); void update(int value) => payload.value = value;extension ChangeValue on ExtensionTarget { void changeValue(int newValue) { void changeExplicitly(int newValue) => this.value = newValue;Valid
class Rectangle {
num _width = 0;
num _height = 0;
set width(num value) => _width = value;
num get width => _width;
// Not a plain assignment of the parameter.
void doubleHeight(num value) => _height = value * 2;
// More than one statement.
void setBoth(num value) {
_width = value;
_height = value;
}
// Compound assignment.
void grow(num value) => _height += value;
// Non-void return type.
num setHeight(num value) => _height = value;
// Two parameters.
void resize(num w, num h) => _width = w;
num get height => _height;
}
// Overriding an inherited method.
abstract class Settable {
void setValue(int value);
}
class Impl implements Settable {
int value = 0;
@override
void setValue(int value) => this.value = value;
}
// Assigning a top-level variable is not a property change.
int globalCount = 0;
class Counter {
void setGlobal(int value) => globalCount = value;
}
// Implementing an SDK interface member.
class Recorder implements Sink<int> {
int last = 0;
void add(int data) {
last = data;
}
void close() {}
}
class ExtensionTarget {
int value = 0;
}
extension ChangeValue on ExtensionTarget {
void updateLocal(int newValue) {
var value = 0;
value = newValue;
}
void updateGlobal(int newValue) => globalCount = newValue;
}Configure use-setters-to-change-properties
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"use-setters-to-change-properties": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/use-setters-to-change-properties: <reason>More style rules
- use-spacer-as-expanded-child
use_spacer_as_expanded_childrecommendedflutterFlags an
Expandedwrapping an emptySizedBoxorContainerthat should be aSpacer. - use-string-in-part-of-directives
use_string_in_part_of_directivesrecommendedFlags a
part ofdirective that names its library instead of using a string URI. - use-super-parameters
use_super_parametersrecommendedPrefer super parameters when a constructor only forwards parameters to its superclass.
- use-test-throws-matchers
use_test_throws_matchersrecommendedPrefers
throwsAmatchers overtry { ...; fail(...); } catch (...) {}. - use-to-and-as-if-applicable
use_to_and_as_if_applicableFlags conversion methods not named
to…oras…. - always-declare-return-types
always_declare_return_typesrecommendedFlags functions and methods declared without a return type.
- always-put-control-body-on-new-line
always_put_control_body_on_new_lineFlags a control-flow body that starts on the same line as its control clause.
- always-put-required-named-parameters-first
always_put_required_named_parameters_firstFlags a required named parameter declared after an optional named one.