Table constructor Null safety
- {Key? key,
- List<
TableRow> children = const <TableRow>[], - Map<
int, TableColumnWidth> ? columnWidths, - TableColumnWidth defaultColumnWidth = const FlexColumnWidth(),
- TextDirection? textDirection,
- TableBorder? border,
- TableCellVerticalAlignment defaultVerticalAlignment = TableCellVerticalAlignment.top,
- TextBaseline? textBaseline}
Creates a table.
The children, defaultColumnWidth, and defaultVerticalAlignment arguments must not be null.
Implementation
Table({
super.key,
this.children = const <TableRow>[],
this.columnWidths,
this.defaultColumnWidth = const FlexColumnWidth(),
this.textDirection,
this.border,
this.defaultVerticalAlignment = TableCellVerticalAlignment.top,
this.textBaseline, // NO DEFAULT: we don't know what the text's baseline should be
}) : assert(children != null),
assert(defaultColumnWidth != null),
assert(defaultVerticalAlignment != null),
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 row) => row.children!.any((Widget cell) => cell == null))) {
throw FlutterError(
'One of the children of one of the rows of the table was null.\n'
'The children of a 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(
'Two or more TableRow children of this Table had the same key.\n'
'All the keyed TableRow children of a Table must have different Keys.',
);
}
return true;
}()),
assert(() {
if (children.isNotEmpty) {
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. '
'Otherwise, the table will contain holes.',
);
}
}
return true;
}()),
_rowDecorations = children.any((TableRow row) => row.decoration != null)
? 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);
if (debugChildrenHaveDuplicateKeys(this, flatChildren)) {
throw FlutterError(
'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 '
'flattened out for processing, so separate cells cannot have duplicate keys even if they are in '
'different rows.',
);
}
return true;
}());
}