Skip to main content

consistent-update-render-object

added in: 1.6.0
preset: recommended

Warns when an updateRenderObject method is absent or doesn't update all fields set in createRenderObject.

Example

❌ Bad:

// LINT
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
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,
);
}


// LINT
void updateRenderObject(
BuildContext context,
_RenderDecoration renderObject,
) {
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;
}
}