Unverified Commit 06a35b33 authored by Andre's avatar Andre Committed by GitHub

Improve error reporting for RenderObject visitChildren errors (#61455)

* Improve error reporting for RenderObject visitChildren errors

* Nits and fixes
parent fdda8059
......@@ -2243,6 +2243,37 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
return;
assert(() {
if (_needsCompositingBitsUpdate) {
if (parent is RenderObject) {
final RenderObject parent = this.parent as RenderObject;
bool visitedByParent = false;
parent.visitChildren((RenderObject child) {
if (child == this) {
visitedByParent = true;
}
});
if (!visitedByParent) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary(
"A RenderObject was not visited by the parent's visitChildren "
'during paint.',
),
parent.describeForError(
'The parent was',
),
describeForError(
'The child that was not visited was'
),
ErrorDescription(
'A RenderObject with children must implement visitChildren and '
'call the visitor exactly once for each child; it also should not '
'paint children that were removed with dropChild.'
),
ErrorHint(
'This usually indicates an error in the Flutter framework itself.'
),
]);
}
}
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary(
'Tried to paint a RenderObject before its compositing bits were '
......
......@@ -8,6 +8,8 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import '../rendering/recording_canvas.dart';
final BoxDecoration kBoxDecorationA = BoxDecoration(border: nonconst(null));
final BoxDecoration kBoxDecorationB = BoxDecoration(border: nonconst(null));
final BoxDecoration kBoxDecorationC = BoxDecoration(border: nonconst(null));
......@@ -48,6 +50,31 @@ class TestOrientedBox extends SingleChildRenderObjectWidget {
}
}
class TestNonVisitingWidget extends SingleChildRenderObjectWidget {
const TestNonVisitingWidget({ Key key, Widget child }) : super(key: key, child: child);
@override
RenderObject createRenderObject(BuildContext context) => TestNonVisitingRenderObject();
}
class TestNonVisitingRenderObject extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
@override
void performLayout() {
child.layout(constraints, parentUsesSize: true);
size = child.size;
}
@override
void paint(PaintingContext context, Offset offset) {
context.paintChild(child, offset);
}
@override
void visitChildren(RenderObjectVisitor visitor) {
// oops!
}
}
void main() {
testWidgets('RenderObjectWidget smoke test', (WidgetTester tester) async {
await tester.pumpWidget(DecoratedBox(decoration: kBoxDecorationA));
......@@ -215,4 +242,25 @@ void main() {
decoration = renderBox.decoration as BoxDecoration;
expect(decoration.color, equals(const Color(0xFF0000FF)));
});
testWidgets('RenderObject not visiting children provides helpful error message', (WidgetTester tester) async {
await tester.pumpWidget(
TestNonVisitingWidget(
child: Container(color: const Color(0xFFED1D7F)),
),
);
final RenderObject renderObject = tester.renderObject(find.byType(TestNonVisitingWidget));
final Canvas testCanvas = TestRecordingCanvas();
final PaintingContext testContext = TestRecordingPaintingContext(testCanvas);
// When a parent fails to visit a child in visitChildren, the child's compositing
// bits won't be cleared properly, leading to an exception during paint.
renderObject.paint(testContext, Offset.zero);
final dynamic error = tester.takeException();
expect(error, isNotNull, reason: 'RenderObject did not throw when painting');
expect(error, isFlutterError);
expect(error.toString(), contains("A RenderObject was not visited by the parent's visitChildren"));
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment