From be2388595e86726ce4411c62f8104982b592e7f4 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Fri, 31 Mar 2023 01:26:51 +0300 Subject: [PATCH] Fix `TableRow` with no children throws unnamed assertion (#123770) Fix `TableRow` with no children throws unnamed assertion --- packages/flutter/lib/src/widgets/table.dart | 6 +++ packages/flutter/test/widgets/table_test.dart | 46 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/packages/flutter/lib/src/widgets/table.dart b/packages/flutter/lib/src/widgets/table.dart index 32d04da3c4..83d54be54f 100644 --- a/packages/flutter/lib/src/widgets/table.dart +++ b/packages/flutter/lib/src/widgets/table.dart @@ -144,6 +144,12 @@ class Table extends RenderObjectWidget { 'Otherwise, the table will contain holes.', ); } + if (children.any((TableRow row) => row.children.isEmpty)) { + throw FlutterError( + 'One or more TableRow have no children.\n' + 'Every TableRow in a Table must have at least one child, so there is no empty row. ', + ); + } } return true; }()), diff --git a/packages/flutter/test/widgets/table_test.dart b/packages/flutter/test/widgets/table_test.dart index ecf38d7ccd..727fd1040d 100644 --- a/packages/flutter/test/widgets/table_test.dart +++ b/packages/flutter/test/widgets/table_test.dart @@ -45,6 +45,7 @@ void main() { ), ); }); + testWidgets('Table widget - control test', (WidgetTester tester) async { Future run(TextDirection textDirection) async { await tester.pumpWidget( @@ -1033,5 +1034,50 @@ void main() { expect(find.text('Hello'), findsOneWidget); }); + testWidgets('TableRow with no children throws an error message', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/119541. + String result = 'no exception'; + + // Test TableRow with children. + try { + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: Table( + children: const [ + TableRow( + children: [ + Text('A'), + ], + ), + ], + ), + )); + } on FlutterError catch (e) { + result = e.toString(); + } + + expect(result, 'no exception'); + + // Test TableRow with no children. + try { + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: Table( + children: const [ + TableRow(), + ], + ), + )); + } on FlutterError catch (e) { + result = e.toString(); + } + + expect( + result, + 'One or more TableRow have no children.\n' + 'Every TableRow in a Table must have at least one child, so there is no empty row.', + ); + }); + // TODO(ianh): Test handling of TableCell object }