Skip to content

Commit

Permalink
Fix nullability of TableRow.children (#119285)
Browse files Browse the repository at this point in the history
* Fix nullablility of TableRow.children

* fix test
  • Loading branch information
goderbauer authored Jan 27, 2023
1 parent 0b57596 commit 0417f66
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
8 changes: 4 additions & 4 deletions packages/flutter/lib/src/material/data_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ class DataTable extends StatelessWidget {
int displayColumnIndex = 0;
if (displayCheckboxColumn) {
tableColumns[0] = FixedColumnWidth(effectiveCheckboxHorizontalMarginStart + Checkbox.width + effectiveCheckboxHorizontalMarginEnd);
tableRows[0].children![0] = _buildCheckbox(
tableRows[0].children[0] = _buildCheckbox(
context: context,
checked: someChecked ? null : allChecked,
onRowTap: null,
Expand All @@ -977,7 +977,7 @@ class DataTable extends StatelessWidget {
);
rowIndex = 1;
for (final DataRow row in rows) {
tableRows[rowIndex].children![0] = _buildCheckbox(
tableRows[rowIndex].children[0] = _buildCheckbox(
context: context,
checked: row.selected,
onRowTap: row.onSelectChanged == null ? null : () => row.onSelectChanged?.call(!row.selected),
Expand Down Expand Up @@ -1020,7 +1020,7 @@ class DataTable extends StatelessWidget {
} else {
tableColumns[displayColumnIndex] = const IntrinsicColumnWidth();
}
tableRows[0].children![displayColumnIndex] = _buildHeadingCell(
tableRows[0].children[displayColumnIndex] = _buildHeadingCell(
context: context,
padding: padding,
label: column.label,
Expand All @@ -1034,7 +1034,7 @@ class DataTable extends StatelessWidget {
rowIndex = 1;
for (final DataRow row in rows) {
final DataCell cell = row.cells[dataColumnIndex];
tableRows[rowIndex].children![displayColumnIndex] = _buildDataCell(
tableRows[rowIndex].children[displayColumnIndex] = _buildDataCell(
context: context,
padding: padding,
label: cell.child,
Expand Down
33 changes: 11 additions & 22 deletions packages/flutter/lib/src/widgets/table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export 'package:flutter/rendering.dart' show
@immutable
class TableRow {
/// Creates a row in a [Table].
const TableRow({ this.key, this.decoration, this.children });
const TableRow({ this.key, this.decoration, this.children = const <Widget>[]});

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

@override
String toString() {
Expand All @@ -61,9 +61,7 @@ class TableRow {
if (decoration != null) {
result.write('$decoration, ');
}
if (children == null) {
result.write('child list is null');
} else if (children!.isEmpty) {
if (children.isEmpty) {
result.write('no children');
} else {
result.write('$children');
Expand Down Expand Up @@ -127,15 +125,6 @@ class Table extends RenderObjectWidget {
this.defaultVerticalAlignment = TableCellVerticalAlignment.top,
this.textBaseline, // NO DEFAULT: we don't know what the text's baseline should be
}) : assert(defaultVerticalAlignment != TableCellVerticalAlignment.baseline || textBaseline != null, 'textBaseline is required if you specify the defaultVerticalAlignment with TableCellVerticalAlignment.baseline'),
assert(() {
if (children.any((TableRow row) => row.children == null)) {
throw FlutterError(
'One of the rows of the table had null children.\n'
'The children property of TableRow must not be null.',
);
}
return true;
}()),
assert(() {
if (children.any((TableRow row1) => row1.key != null && children.any((TableRow row2) => row1 != row2 && row1.key == row2.key))) {
throw FlutterError(
Expand All @@ -147,8 +136,8 @@ class Table extends RenderObjectWidget {
}()),
assert(() {
if (children.isNotEmpty) {
final int cellCount = children.first.children!.length;
if (children.any((TableRow row) => row.children!.length != cellCount)) {
final int cellCount = children.first.children.length;
if (children.any((TableRow row) => row.children.length != cellCount)) {
throw FlutterError(
'Table contains irregular row lengths.\n'
'Every TableRow in a Table must have the same number of children, so that every cell is filled. '
Expand All @@ -162,7 +151,7 @@ class Table extends RenderObjectWidget {
? children.map<Decoration?>((TableRow row) => row.decoration).toList(growable: false)
: null {
assert(() {
final List<Widget> flatChildren = children.expand<Widget>((TableRow row) => row.children!).toList(growable: false);
final List<Widget> flatChildren = children.expand<Widget>((TableRow row) => row.children).toList(growable: false);
return !debugChildrenHaveDuplicateKeys(this, flatChildren, message:
'Two or more cells in this Table contain widgets with the same key.\n'
'Every widget child of every TableRow in a Table must have different keys. The cells of a Table are '
Expand Down Expand Up @@ -238,7 +227,7 @@ class Table extends RenderObjectWidget {
RenderTable createRenderObject(BuildContext context) {
assert(debugCheckHasDirectionality(context));
return RenderTable(
columns: children.isNotEmpty ? children[0].children!.length : 0,
columns: children.isNotEmpty ? children[0].children.length : 0,
rows: children.length,
columnWidths: columnWidths,
defaultColumnWidth: defaultColumnWidth,
Expand All @@ -254,7 +243,7 @@ class Table extends RenderObjectWidget {
@override
void updateRenderObject(BuildContext context, RenderTable renderObject) {
assert(debugCheckHasDirectionality(context));
assert(renderObject.columns == (children.isNotEmpty ? children[0].children!.length : 0));
assert(renderObject.columns == (children.isNotEmpty ? children[0].children.length : 0));
assert(renderObject.rows == children.length);
renderObject
..columnWidths = columnWidths
Expand Down Expand Up @@ -289,7 +278,7 @@ class _TableElement extends RenderObjectElement {
rowIndex += 1;
return _TableElementRow(
key: row.key,
children: row.children!.map<Element>((Widget child) {
children: row.children.map<Element>((Widget child) {
return inflateWidget(child, _TableSlot(columnIndex++, rowIndex));
}).toList(growable: false),
);
Expand Down Expand Up @@ -347,12 +336,12 @@ class _TableElement extends RenderObjectElement {
oldChildren = const <Element>[];
}
final List<_TableSlot> slots = List<_TableSlot>.generate(
row.children!.length,
row.children.length,
(int columnIndex) => _TableSlot(columnIndex, rowIndex),
);
newChildren.add(_TableElementRow(
key: row.key,
children: updateChildren(oldChildren, row.children!, forgottenChildren: _forgottenChildren, slots: slots),
children: updateChildren(oldChildren, row.children, forgottenChildren: _forgottenChildren, slots: slots),
));
}
while (oldUnkeyedRows.moveNext()) {
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter/test/widgets/table_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ void main() {
});

testWidgets(
'Table widget requires all TableRows to have non-null children',
'Table widget requires all TableRows to have same number of children',
(WidgetTester tester) async {
FlutterError? error;
try {
Expand All @@ -959,7 +959,7 @@ void main() {
error = e;
} finally {
expect(error, isNotNull);
expect(error!.toStringDeep(), contains('The children property of TableRow must not be null.'));
expect(error!.toStringDeep(), contains('Table contains irregular row lengths.'));
}
},
);
Expand Down

0 comments on commit 0417f66

Please sign in to comment.