Add assert for negative child count in ListView.builder (#45506)
This commit is contained in:
@@ -81,6 +81,7 @@ abstract class ScrollView extends StatelessWidget {
|
||||
assert(!shrinkWrap || center == null),
|
||||
assert(anchor != null),
|
||||
assert(anchor >= 0.0 && anchor <= 1.0),
|
||||
assert(semanticChildCount == null || semanticChildCount >= 0),
|
||||
primary = primary ?? controller == null && identical(scrollDirection, Axis.vertical),
|
||||
physics = physics ?? (primary == true || (primary == null && controller == null && identical(scrollDirection, Axis.vertical)) ? const AlwaysScrollableScrollPhysics() : null),
|
||||
super(key: key);
|
||||
@@ -943,7 +944,9 @@ class ListView extends BoxScrollView {
|
||||
double cacheExtent,
|
||||
int semanticChildCount,
|
||||
DragStartBehavior dragStartBehavior = DragStartBehavior.start,
|
||||
}) : childrenDelegate = SliverChildBuilderDelegate(
|
||||
}) : assert(itemCount == null || itemCount >= 0),
|
||||
assert(semanticChildCount == null || semanticChildCount <= itemCount),
|
||||
childrenDelegate = SliverChildBuilderDelegate(
|
||||
itemBuilder,
|
||||
childCount: itemCount,
|
||||
addAutomaticKeepAlives: addAutomaticKeepAlives,
|
||||
|
||||
@@ -88,6 +88,7 @@ class Scrollable extends StatefulWidget {
|
||||
assert(dragStartBehavior != null),
|
||||
assert(viewportBuilder != null),
|
||||
assert(excludeFromSemantics != null),
|
||||
assert(semanticChildCount == null || semanticChildCount >= 0),
|
||||
super (key: key);
|
||||
|
||||
/// The direction in which this widget scrolls.
|
||||
@@ -643,6 +644,7 @@ class _ScrollSemantics extends SingleChildRenderObjectWidget {
|
||||
@required this.semanticChildCount,
|
||||
Widget child,
|
||||
}) : assert(position != null),
|
||||
assert(semanticChildCount == null || semanticChildCount >= 0),
|
||||
super(key: key, child: child);
|
||||
|
||||
final ScrollPosition position;
|
||||
|
||||
@@ -559,4 +559,33 @@ void main() {
|
||||
expect(tester.takeException(), isInstanceOf<Exception>());
|
||||
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>()));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user