Skip to main content

check-for-equals-in-render-object-setters

added in: 1.6.0
preset: recommended

Warns when a RenderObject setter doesn't have an equality check for the new value.

The early return with the equality check is very helpful, because set a is always called within updateRenderObject. If it does not early return, then the markNeedsPaint (or for other fields, maybe markNeedsLayout, some heavy work, etc), will be unconditionally executed every time the render object is updated, even if that is totally unnecessary.

Example

❌ Bad:

class SomeRenderBox extends RenderBox {
double _dividerWidth;
double get dividerWidth => _dividerWidth;
// LINT
set dividerWidth(double value) {
_dividerWidth = value;
markNeedsLayout();
}

double get spacing => _spacing;
double _spacing;
// LINT
set spacing(double value) {
_spacing = value;

if (_spacing == value) {
return;
}
markNeedsLayout();
}
}

✅ Good:

class SomeRenderBox extends RenderBox {
double get overflowSpacing => _overflowSpacing;
double _overflowSpacing;
set overflowSpacing(double value) {
assert(value != null);
if (_overflowSpacing == value) return;

_overflowSpacing = value;
markNeedsLayout();
}
}