Unverified Commit cf37c2cd authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Add assert for negative child count in ListView.builder (#45506)

parent 1888fa35
...@@ -81,6 +81,7 @@ abstract class ScrollView extends StatelessWidget { ...@@ -81,6 +81,7 @@ abstract class ScrollView extends StatelessWidget {
assert(!shrinkWrap || center == null), assert(!shrinkWrap || center == null),
assert(anchor != null), assert(anchor != null),
assert(anchor >= 0.0 && anchor <= 1.0), assert(anchor >= 0.0 && anchor <= 1.0),
assert(semanticChildCount == null || semanticChildCount >= 0),
primary = primary ?? controller == null && identical(scrollDirection, Axis.vertical), primary = primary ?? controller == null && identical(scrollDirection, Axis.vertical),
physics = physics ?? (primary == true || (primary == null && controller == null && identical(scrollDirection, Axis.vertical)) ? const AlwaysScrollableScrollPhysics() : null), physics = physics ?? (primary == true || (primary == null && controller == null && identical(scrollDirection, Axis.vertical)) ? const AlwaysScrollableScrollPhysics() : null),
super(key: key); super(key: key);
...@@ -943,7 +944,9 @@ class ListView extends BoxScrollView { ...@@ -943,7 +944,9 @@ class ListView extends BoxScrollView {
double cacheExtent, double cacheExtent,
int semanticChildCount, int semanticChildCount,
DragStartBehavior dragStartBehavior = DragStartBehavior.start, DragStartBehavior dragStartBehavior = DragStartBehavior.start,
}) : childrenDelegate = SliverChildBuilderDelegate( }) : assert(itemCount == null || itemCount >= 0),
assert(semanticChildCount == null || semanticChildCount <= itemCount),
childrenDelegate = SliverChildBuilderDelegate(
itemBuilder, itemBuilder,
childCount: itemCount, childCount: itemCount,
addAutomaticKeepAlives: addAutomaticKeepAlives, addAutomaticKeepAlives: addAutomaticKeepAlives,
......
...@@ -88,6 +88,7 @@ class Scrollable extends StatefulWidget { ...@@ -88,6 +88,7 @@ class Scrollable extends StatefulWidget {
assert(dragStartBehavior != null), assert(dragStartBehavior != null),
assert(viewportBuilder != null), assert(viewportBuilder != null),
assert(excludeFromSemantics != null), assert(excludeFromSemantics != null),
assert(semanticChildCount == null || semanticChildCount >= 0),
super (key: key); super (key: key);
/// The direction in which this widget scrolls. /// The direction in which this widget scrolls.
...@@ -643,6 +644,7 @@ class _ScrollSemantics extends SingleChildRenderObjectWidget { ...@@ -643,6 +644,7 @@ class _ScrollSemantics extends SingleChildRenderObjectWidget {
@required this.semanticChildCount, @required this.semanticChildCount,
Widget child, Widget child,
}) : assert(position != null), }) : assert(position != null),
assert(semanticChildCount == null || semanticChildCount >= 0),
super(key: key, child: child); super(key: key, child: child);
final ScrollPosition position; final ScrollPosition position;
......
...@@ -559,4 +559,33 @@ void main() { ...@@ -559,4 +559,33 @@ void main() {
expect(tester.takeException(), isInstanceOf<Exception>()); expect(tester.takeException(), isInstanceOf<Exception>());
expect(finder, findsOneWidget); expect(finder, findsOneWidget);
}); });
testWidgets('ListView.builder asserts on negative childCount', (WidgetTester tester) async {
expect(() => ListView.builder(
itemBuilder: (BuildContext context, int index) {
return const SizedBox();
},
itemCount: -1,
), throwsA(isInstanceOf<AssertionError>()));
});
testWidgets('ListView.builder asserts on negative semanticChildCount', (WidgetTester tester) async {
expect(() => ListView.builder(
itemBuilder: (BuildContext context, int index) {
return const SizedBox();
},
itemCount: 1,
semanticChildCount: -1,
), throwsA(isInstanceOf<AssertionError>()));
});
testWidgets('ListView.builder asserts on nonsensical childCount/semanticChildCount', (WidgetTester tester) async {
expect(() => ListView.builder(
itemBuilder: (BuildContext context, int index) {
return const SizedBox();
},
itemCount: 1,
semanticChildCount: 4,
), throwsA(isInstanceOf<AssertionError>()));
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment