diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index c7e1649fb1..872402d9d7 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -5944,8 +5944,7 @@ abstract class RenderObjectElement extends Element { int newChildrenBottom = newWidgets.length - 1; int oldChildrenBottom = oldChildren.length - 1; - final List newChildren = oldChildren.length == newWidgets.length ? - oldChildren : List.filled(newWidgets.length, _NullElement.instance); + final List newChildren = List.filled(newWidgets.length, _NullElement.instance); Element? previousChild; diff --git a/packages/flutter/test/widgets/framework_test.dart b/packages/flutter/test/widgets/framework_test.dart index ea2e49b17c..d884e85210 100644 --- a/packages/flutter/test/widgets/framework_test.dart +++ b/packages/flutter/test/widgets/framework_test.dart @@ -1753,6 +1753,30 @@ The findRenderObject() method was called for the following element: child.dependOnInheritedElement(ancestor); expect(child.doesDependOnInheritedElement(ancestor), isTrue); }); + + testWidgets( + 'MultiChildRenderObjectElement.updateChildren test', + (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/120762. + final GlobalKey globalKey = GlobalKey(); + await tester.pumpWidget(Column( + children: [ + const SizedBox(), + SizedBox(key: globalKey), + const SizedBox(), + ], + )); + expect(tester.takeException(), isNull); + + await tester.pumpWidget(Column( + children: [ + const SizedBox(), + const SizedBox(), + SizedBox(child: SizedBox(key: globalKey)), + ], + )); + expect(tester.takeException(), isNull); + }); } class _TestInheritedElement extends InheritedElement {