Skip to content

Commit

Permalink
Modify the updateChildren method deep copy _children (#120773)
Browse files Browse the repository at this point in the history
* Modify the updateChildren method deep copy _children

* add test

* fix some small nits

* Simplified newChildren declaration in updateChildren
  • Loading branch information
yiiim authored Feb 17, 2023
1 parent 674254c commit c4d40cc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 1 addition & 2 deletions packages/flutter/lib/src/widgets/framework.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5944,8 +5944,7 @@ abstract class RenderObjectElement extends Element {
int newChildrenBottom = newWidgets.length - 1;
int oldChildrenBottom = oldChildren.length - 1;

final List<Element> newChildren = oldChildren.length == newWidgets.length ?
oldChildren : List<Element>.filled(newWidgets.length, _NullElement.instance);
final List<Element> newChildren = List<Element>.filled(newWidgets.length, _NullElement.instance);

Element? previousChild;

Expand Down
24 changes: 24 additions & 0 deletions packages/flutter/test/widgets/framework_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: <Widget>[
const SizedBox(),
SizedBox(key: globalKey),
const SizedBox(),
],
));
expect(tester.takeException(), isNull);

await tester.pumpWidget(Column(
children: <Widget>[
const SizedBox(),
const SizedBox(),
SizedBox(child: SizedBox(key: globalKey)),
],
));
expect(tester.takeException(), isNull);
});
}

class _TestInheritedElement extends InheritedElement {
Expand Down

0 comments on commit c4d40cc

Please sign in to comment.