diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index 440ca394d7..220f974c92 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -3418,19 +3418,22 @@ abstract class Element extends DiagnosticableTree implements BuildContext { /// this location in the tree. Otherwise, this getter will walk down the tree /// until it finds a [RenderObjectElement]. RenderObject? get renderObject { - RenderObject? result; - void visit(Element element) { - assert(result == null); // this verifies that there's only one child - if (element._lifecycleState == _ElementLifecycle.defunct) { - return; - } else if (element is RenderObjectElement) { - result = element.renderObject; + Element? current = this; + while (current != null) { + if (current._lifecycleState == _ElementLifecycle.defunct) { + break; + } else if (current is RenderObjectElement) { + return current.renderObject; } else { - element.visitChildren(visit); + Element? next; + current.visitChildren((Element child) { + assert(next == null); // This verifies that there's only one child. + next = child; + }); + current = next; } } - visit(this); - return result; + return null; } @override