consistent-update-render-object
preset: recommended
Warns when an updateRenderObject
method is absent or doesn't update all fields set in createRenderObject
.
Example
❌ Bad:
// LINT: Missing implementation for the 'updateRenderObject' method. Try adding it.
class ColorFiltered extends SingleChildRenderObjectWidget {
const ColorFiltered({required this.value});
final int value;
RenderObject createRenderObject(BuildContext context) =>
_ColorFilterRenderObject(colorFilter);
}
✅ Good:
class ColorFiltered extends SingleChildRenderObjectWidget {
const ColorFiltered({required this.value});
final int value;
RenderObject createRenderObject(BuildContext context) =>
_ColorFilterRenderObject(colorFilter);
void updateRenderObject(BuildContext context, RenderObject renderObject) {
(renderObject as _ColorFilterRenderObject).value = value;
}
}
❌ Bad:
class _MenuItem extends SingleChildRenderObjectWidget {
const _MenuItem({required this.value});
final bool value;
RenderObject createRenderObject(BuildContext context) {
return _RenderMenuItem(value);
}
// LINT: 'updateRenderObject' method doesn't update all parameters, that are set in 'createRenderObject'.
void updateRenderObject(BuildContext context, _RenderMenuItem renderObject) {}
}
✅ Good:
class _MenuItem extends SingleChildRenderObjectWidget {
const _MenuItem({required this.value});
final bool value;
RenderObject createRenderObject(BuildContext context) {
return _RenderMenuItem(value);
}
void updateRenderObject(BuildContext context, _RenderMenuItem renderObject) {
renderObject.value = value;
}
}
❌ Bad:
class _Decorator extends RenderObjectWidget {
const _Decorator({
required this.textDirection,
required this.isFocused,
required this.expands,
});
final TextDirection textDirection;
final bool isFocused;
final bool expands;
_RenderDecoration createRenderObject(BuildContext context) {
return _RenderDecoration(
textDirection: textDirection,
isFocused: isFocused,
expands: expands,
);
}
void updateRenderObject(
BuildContext context,
_RenderDecoration renderObject,
) {
// LINT: 'updateRenderObject' method doesn't update all parameters, that are set in 'createRenderObject'.
renderObject
..expands = expands
..textDirection = textDirection;
}
}
✅ Good:
class _Decorator extends RenderObjectWidget {
const _Decorator({
required this.textDirection,
required this.isFocused,
required this.expands,
});
final TextDirection textDirection;
final bool isFocused;
final bool expands;
_RenderDecoration createRenderObject(BuildContext context) {
return _RenderDecoration(
textDirection: textDirection,
isFocused: isFocused,
expands: expands,
);
}
void updateRenderObject(
BuildContext context,
_RenderDecoration renderObject,
) {
renderObject
..expands = expands
..isFocused = isFocused
..textDirection = textDirection;
}
}