From d27a942730fdddf5d2224c2c7ccee9f0f459cee2 Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Tue, 18 May 2021 00:24:03 -0700 Subject: [PATCH] Do not crash if table children are replaced before they are layed out (#82765) --- packages/flutter/lib/src/widgets/table.dart | 3 +- packages/flutter/test/widgets/table_test.dart | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/widgets/table.dart b/packages/flutter/lib/src/widgets/table.dart index 7fdb922f8f..85eeb9acdb 100644 --- a/packages/flutter/lib/src/widgets/table.dart +++ b/packages/flutter/lib/src/widgets/table.dart @@ -392,8 +392,7 @@ class _TableElement extends RenderObjectElement { @override void removeRenderObjectChild(RenderBox child, _TableSlot slot) { - final TableCellParentData childParentData = child.parentData! as TableCellParentData; - renderObject.setChild(childParentData.x!, childParentData.y!, null); + renderObject.setChild(slot.column, slot.row, null); } final Set _forgottenChildren = HashSet(); diff --git a/packages/flutter/test/widgets/table_test.dart b/packages/flutter/test/widgets/table_test.dart index 5d53f44237..a6dd613233 100644 --- a/packages/flutter/test/widgets/table_test.dart +++ b/packages/flutter/test/widgets/table_test.dart @@ -1000,5 +1000,38 @@ void main() { expect(table.column(2).last.runtimeType, isNot(toBeReplaced)); }); + testWidgets('Do not crash if a child that has not been layed out in a previous build is removed', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/60488. + Widget buildTable(Key key) { + return Directionality( + textDirection: TextDirection.ltr, + child: Table( + children: [ + TableRow( + children: [ + KeyedSubtree( + key: key, + child: const Text('Hello'), + ), + ], + ), + ], + ), + ); + } + + await tester.pumpWidget( + buildTable(const ValueKey(1)), + null, EnginePhase.build, // Children are not layed out! + ); + + await tester.pumpWidget( + buildTable(const ValueKey(2)), + ); + + expect(tester.takeException(), isNull); + expect(find.text('Hello'), findsOneWidget); + }); + // TODO(ianh): Test handling of TableCell object }