Add assertion of recordable list (#76272)

This commit is contained in:
Abhishek Ghaskata 2021-03-17 00:48:01 +05:30 committed by GitHub
parent 5ab3474435
commit 43e3328e98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View file

@ -457,6 +457,14 @@ class _ReorderableListContentState extends State<_ReorderableListContent> {
Widget _itemBuilder(BuildContext context, int index) {
final Widget item = widget.itemBuilder(context, index);
assert(() {
if (item.key == null) {
throw FlutterError(
'Every item of ReorderableListView must have a key.'
);
}
return true;
}());
// TODO(goderbauer): The semantics stuff should probably happen inside
// _ReorderableItem so the widget versions can have them as well.

View file

@ -1326,6 +1326,22 @@ void main() {
}
expect(items.take(8), orderedEquals(<int>[0, 1, 2, 3, 4, 5, 6, 7]));
});
testWidgets('ReorderableListView throws an error when key is not passed to its children', (WidgetTester tester) async {
final Widget reorderableListView = ReorderableListView.builder(
itemBuilder: (BuildContext context, int index) {
return SizedBox(child: Text('Item $index'));
},
itemCount: 3,
onReorder: (int oldIndex, int newIndex) { },
);
await tester.pumpWidget(MaterialApp(
home: reorderableListView,
));
final dynamic exception = tester.takeException();
expect(exception, isFlutterError);
expect(exception.toString(), contains('Every item of ReorderableListView must have a key.'));
});
}
Future<void> longPressDrag(WidgetTester tester, Offset start, Offset end) async {