Skip to main content

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

added in: 1.6.0
Free+
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: Equals check is missing. Try adding the missing check.
set dividerWidth(double value) {
_dividerWidth = value;
markNeedsLayout();
}

double _spacing;
double get spacing => _spacing;

set spacing(double value) {
_spacing = value;

// LINT: Equals check should come first in the block. Try moving it up.
if (_spacing == value) {
return;
}
markNeedsLayout();
}
}

✅ Good:

class SomeRenderBox extends RenderBox {
double _overflowSpacing;

double get overflowSpacing => _overflowSpacing;
set overflowSpacing(double value) {
if (_overflowSpacing == value) return;

_overflowSpacing = value;
markNeedsLayout();
}
}