Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 0417f66

Browse files
authored
Fix nullability of TableRow.children (#119285)
* Fix nullablility of TableRow.children * fix test
1 parent 0b57596 commit 0417f66

File tree

3 files changed

+17
-28
lines changed

3 files changed

+17
-28
lines changed

packages/flutter/lib/src/material/data_table.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ class DataTable extends StatelessWidget {
967967
int displayColumnIndex = 0;
968968
if (displayCheckboxColumn) {
969969
tableColumns[0] = FixedColumnWidth(effectiveCheckboxHorizontalMarginStart + Checkbox.width + effectiveCheckboxHorizontalMarginEnd);
970-
tableRows[0].children![0] = _buildCheckbox(
970+
tableRows[0].children[0] = _buildCheckbox(
971971
context: context,
972972
checked: someChecked ? null : allChecked,
973973
onRowTap: null,
@@ -977,7 +977,7 @@ class DataTable extends StatelessWidget {
977977
);
978978
rowIndex = 1;
979979
for (final DataRow row in rows) {
980-
tableRows[rowIndex].children![0] = _buildCheckbox(
980+
tableRows[rowIndex].children[0] = _buildCheckbox(
981981
context: context,
982982
checked: row.selected,
983983
onRowTap: row.onSelectChanged == null ? null : () => row.onSelectChanged?.call(!row.selected),
@@ -1020,7 +1020,7 @@ class DataTable extends StatelessWidget {
10201020
} else {
10211021
tableColumns[displayColumnIndex] = const IntrinsicColumnWidth();
10221022
}
1023-
tableRows[0].children![displayColumnIndex] = _buildHeadingCell(
1023+
tableRows[0].children[displayColumnIndex] = _buildHeadingCell(
10241024
context: context,
10251025
padding: padding,
10261026
label: column.label,
@@ -1034,7 +1034,7 @@ class DataTable extends StatelessWidget {
10341034
rowIndex = 1;
10351035
for (final DataRow row in rows) {
10361036
final DataCell cell = row.cells[dataColumnIndex];
1037-
tableRows[rowIndex].children![displayColumnIndex] = _buildDataCell(
1037+
tableRows[rowIndex].children[displayColumnIndex] = _buildDataCell(
10381038
context: context,
10391039
padding: padding,
10401040
label: cell.child,

packages/flutter/lib/src/widgets/table.dart

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export 'package:flutter/rendering.dart' show
3232
@immutable
3333
class TableRow {
3434
/// Creates a row in a [Table].
35-
const TableRow({ this.key, this.decoration, this.children });
35+
const TableRow({ this.key, this.decoration, this.children = const <Widget>[]});
3636

3737
/// An identifier for the row.
3838
final LocalKey? key;
@@ -49,7 +49,7 @@ class TableRow {
4949
/// Children may be wrapped in [TableCell] widgets to provide per-cell
5050
/// configuration to the [Table], but children are not required to be wrapped
5151
/// in [TableCell] widgets.
52-
final List<Widget>? children;
52+
final List<Widget> children;
5353

5454
@override
5555
String toString() {
@@ -61,9 +61,7 @@ class TableRow {
6161
if (decoration != null) {
6262
result.write('$decoration, ');
6363
}
64-
if (children == null) {
65-
result.write('child list is null');
66-
} else if (children!.isEmpty) {
64+
if (children.isEmpty) {
6765
result.write('no children');
6866
} else {
6967
result.write('$children');
@@ -127,15 +125,6 @@ class Table extends RenderObjectWidget {
127125
this.defaultVerticalAlignment = TableCellVerticalAlignment.top,
128126
this.textBaseline, // NO DEFAULT: we don't know what the text's baseline should be
129127
}) : assert(defaultVerticalAlignment != TableCellVerticalAlignment.baseline || textBaseline != null, 'textBaseline is required if you specify the defaultVerticalAlignment with TableCellVerticalAlignment.baseline'),
130-
assert(() {
131-
if (children.any((TableRow row) => row.children == null)) {
132-
throw FlutterError(
133-
'One of the rows of the table had null children.\n'
134-
'The children property of TableRow must not be null.',
135-
);
136-
}
137-
return true;
138-
}()),
139128
assert(() {
140129
if (children.any((TableRow row1) => row1.key != null && children.any((TableRow row2) => row1 != row2 && row1.key == row2.key))) {
141130
throw FlutterError(
@@ -147,8 +136,8 @@ class Table extends RenderObjectWidget {
147136
}()),
148137
assert(() {
149138
if (children.isNotEmpty) {
150-
final int cellCount = children.first.children!.length;
151-
if (children.any((TableRow row) => row.children!.length != cellCount)) {
139+
final int cellCount = children.first.children.length;
140+
if (children.any((TableRow row) => row.children.length != cellCount)) {
152141
throw FlutterError(
153142
'Table contains irregular row lengths.\n'
154143
'Every TableRow in a Table must have the same number of children, so that every cell is filled. '
@@ -162,7 +151,7 @@ class Table extends RenderObjectWidget {
162151
? children.map<Decoration?>((TableRow row) => row.decoration).toList(growable: false)
163152
: null {
164153
assert(() {
165-
final List<Widget> flatChildren = children.expand<Widget>((TableRow row) => row.children!).toList(growable: false);
154+
final List<Widget> flatChildren = children.expand<Widget>((TableRow row) => row.children).toList(growable: false);
166155
return !debugChildrenHaveDuplicateKeys(this, flatChildren, message:
167156
'Two or more cells in this Table contain widgets with the same key.\n'
168157
'Every widget child of every TableRow in a Table must have different keys. The cells of a Table are '
@@ -238,7 +227,7 @@ class Table extends RenderObjectWidget {
238227
RenderTable createRenderObject(BuildContext context) {
239228
assert(debugCheckHasDirectionality(context));
240229
return RenderTable(
241-
columns: children.isNotEmpty ? children[0].children!.length : 0,
230+
columns: children.isNotEmpty ? children[0].children.length : 0,
242231
rows: children.length,
243232
columnWidths: columnWidths,
244233
defaultColumnWidth: defaultColumnWidth,
@@ -254,7 +243,7 @@ class Table extends RenderObjectWidget {
254243
@override
255244
void updateRenderObject(BuildContext context, RenderTable renderObject) {
256245
assert(debugCheckHasDirectionality(context));
257-
assert(renderObject.columns == (children.isNotEmpty ? children[0].children!.length : 0));
246+
assert(renderObject.columns == (children.isNotEmpty ? children[0].children.length : 0));
258247
assert(renderObject.rows == children.length);
259248
renderObject
260249
..columnWidths = columnWidths
@@ -289,7 +278,7 @@ class _TableElement extends RenderObjectElement {
289278
rowIndex += 1;
290279
return _TableElementRow(
291280
key: row.key,
292-
children: row.children!.map<Element>((Widget child) {
281+
children: row.children.map<Element>((Widget child) {
293282
return inflateWidget(child, _TableSlot(columnIndex++, rowIndex));
294283
}).toList(growable: false),
295284
);
@@ -347,12 +336,12 @@ class _TableElement extends RenderObjectElement {
347336
oldChildren = const <Element>[];
348337
}
349338
final List<_TableSlot> slots = List<_TableSlot>.generate(
350-
row.children!.length,
339+
row.children.length,
351340
(int columnIndex) => _TableSlot(columnIndex, rowIndex),
352341
);
353342
newChildren.add(_TableElementRow(
354343
key: row.key,
355-
children: updateChildren(oldChildren, row.children!, forgottenChildren: _forgottenChildren, slots: slots),
344+
children: updateChildren(oldChildren, row.children, forgottenChildren: _forgottenChildren, slots: slots),
356345
));
357346
}
358347
while (oldUnkeyedRows.moveNext()) {

packages/flutter/test/widgets/table_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ void main() {
940940
});
941941

942942
testWidgets(
943-
'Table widget requires all TableRows to have non-null children',
943+
'Table widget requires all TableRows to have same number of children',
944944
(WidgetTester tester) async {
945945
FlutterError? error;
946946
try {
@@ -959,7 +959,7 @@ void main() {
959959
error = e;
960960
} finally {
961961
expect(error, isNotNull);
962-
expect(error!.toStringDeep(), contains('The children property of TableRow must not be null.'));
962+
expect(error!.toStringDeep(), contains('Table contains irregular row lengths.'));
963963
}
964964
},
965965
);

0 commit comments

Comments
 (0)