I get IndexError
thrown by LayoutBuilder
.
Error message in VS Code terminal:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following IndexError was thrown building LayoutBuilder:
RangeError (index): Index out of range: no indices are valid: 0
The relevant error-causing widget was:
PaginatedDataTable
PaginatedDataTable:file:///Users/name/development/projects/flutter/project/lib/screens/tabs/a_tab_screen.dart:23:17
When the exception was thrown, this was the stack:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:49 throw_
dart-sdk/lib/_internal/js_dev_runtime/private/js_string.dart 504:7 _get]
packages/project/screens/tabs/a_tab_screen.dart 88:35 getRow
packages/flutter/src/material/paginated_data_table.dart 346:60 <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/identity_hash_map.dart 75:23 putIfAbsent
packages/flutter/src/material/paginated_data_table.dart 346:21 [_getRows]
packages/flutter/src/material/paginated_data_table.dart 539:27 <fn>
packages/flutter/src/material/paginated_data_table.dart 558:48 <fn>
packages/flutter/src/widgets/layout_builder.dart 119:70 layoutCallback
packages/flutter/src/widgets/framework.dart 2605:19 buildScope
packages/flutter/src/widgets/layout_builder.dart 153:5 [_layout]
packages/flutter/src/rendering/object.dart 2246:59 <fn>
packages/flutter/src/rendering/object.dart 1035:15 [_enableMutationsToDirtySubtrees]
packages/flutter/src/rendering/object.dart 2246:7 invokeLayoutCallback
packages/flutter/src/widgets/layout_builder.dart 228:7 rebuildIfNecessary
packages/flutter/src/widgets/layout_builder.dart 316:5 performLayout
packages/flutter/src/rendering/object.dart 2135:7 layout
packages/flutter/src/rendering/box.dart 2418:11 layout
packages/flutter/src/rendering/proxy_box.dart 120:7 performLayout
packages/flutter/src/rendering/object.dart 2135:7 layout
packages/flutter/src/rendering/box.dart 2418:11 layout
packages/flutter/src/rendering/proxy_box.dart 120:7 performLayout
packages/flutter/src/rendering/object.dart 2135:7 layout
packages/flutter/src/rendering/box.dart 2418:11 layout
packages/flutter/src/rendering/proxy_box.dart 120:7 performLayout
packages/flutter/src/rendering/custom_paint.dart 552:11 performLayout
packages/flutter/src/rendering/object.dart 2135:7 layout
packages/flutter/src/rendering/box.dart 2418:11 layout
packages/flutter/src/rendering/proxy_box.dart 120:7 performLayout
packages/flutter/src/rendering/proxy_box.dart 1462:11 performLayout
packages/flutter/src/rendering/object.dart 2135:7 layout
packages/flutter/src/rendering/box.dart 2418:11 layout
packages/flutter/src/rendering/shifted_box.dart 249:5 performLayout
packages/flutter/src/rendering/object.dart 2135:7 layout
packages/flutter/src/rendering/box.dart 2418:11 layout
packages/flutter/src/rendering/proxy_box.dart 120:7 performLayout
packages/flutter/src/rendering/object.dart 2135:7 layout
packages/flutter/src/rendering/box.dart 2418:11 layout
packages/flutter/src/rendering/shifted_box.dart 249:5 performLayout
packages/flutter/src/rendering/object.dart 1973:7 [_layoutWithoutResize]
packages/flutter/src/rendering/object.dart 999:17 flushLayout
packages/flutter/src/rendering/binding.dart 513:19 drawFrame
packages/flutter/src/widgets/binding.dart 884:13 drawFrame
packages/flutter/src/rendering/binding.dart 378:5 [_handlePersistentFrameCallback]
packages/flutter/src/scheduler/binding.dart 1175:15 [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1104:9 handleDrawFrame
packages/flutter/src/scheduler/binding.dart 1015:5 [_handleDrawFrame]
lib/_engine/engine/platform_dispatcher.dart 1168:13 invoke
lib/_engine/engine/platform_dispatcher.dart 219:5 invokeOnDrawFrame
lib/_engine/engine/initialization.dart 195:45 <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14 _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39 dcall
════════════════════════════════════════════════════════════════════════════════════════════════════
My code:
StreamBuilder<List<Model>>(
stream: stream,
builder: (context, snapshot) {
return snapshot.data == null
? Container()
: PaginatedDataTable(
source: MyData(list: snapshot.data!),
columns: [
DataColumn(
label: Text(
"Name",
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
DataColumn(
label: Text(
"Email",
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
],
columnSpacing: 20,
horizontalMargin: 10,
rowsPerPage: 10,
showCheckboxColumn: false,
);
},
),
class MyData extends DataTableSource {
MyData({required this.list}) : super();
final List<Model> list;
@override
bool get isRowCountApproximate => false;
@override
int get rowCount => list.length;
@override
int get selectedRowCount => 0;
@override
DataRow getRow(int index) {
return DataRow(
cells: [
DataCell(
Text(
list[index].name,
),
),
DataCell(
Text(
list[index].email,
),
),
],
);
}
}
Feel free to comment if you need more information!
How to fix this error? I would appreciate any help. Thank you in advance!
You missed the index param in the
MyData
class. ThegetRow()
method must return DataRow usingDataRow.byIndex(…)
and inside thebyIndex()
method you should set theindex
parameter.Take a look at this article.