From c4d40cc1528e5404bf358d908807bad1b98d3e90 Mon Sep 17 00:00:00 2001 From: yim Date: Sat, 18 Feb 2023 02:29:02 +0800 Subject: [PATCH] Modify the updateChildren method deep copy _children (#120773) * Modify the updateChildren method deep copy _children * add test * fix some small nits * Simplified newChildren declaration in updateChildren --- .../flutter/lib/src/widgets/framework.dart | 3 +-- .../flutter/test/widgets/framework_test.dart | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) 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 {