Reputation: 525
Getting a very pesky error on this segment of my Flutter app and no clue why:
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
childAspectRatio: 4 / 3,
mainAxisSpacing: 30.0,
crossAxisSpacing: 20.0),
padding: EdgeInsets.only(left: 20),
scrollDirection: Axis.horizontal,
itemCount: products.length,
itemBuilder: (context, i) => ChangeNotifierProvider.value(
value: products[i],
child: Consumer<Product>(
builder: (context, product, _) {
return ProductCard(
product: product,
onSelected: (prod) {
setState(() {
products.forEach(
(item) {
item.isSelected = false;
},
);
prod.isSelected = true;
});
here's the error: SliverGeometry is not valid: The "scrollExtent" is negative.geometry: SliverGeometry(scrollExtent: -10.0, paintExtent: 20.0, maxPaintExtent: -10.0, cacheExtent: 20.0)
scrollExtent: -10.0
paintExtent: 20.0
maxPaintExtent: -10.0
cacheExtent: 20.0
padding: EdgeInsets(20.0, 0.0, 0.0, 0.0)
This error is preventing emulation on Android even though the apk is installed but it just won't run. It runs on iOS but the error remains.
Upvotes: 2
Views: 2900
Reputation: 446
It is my code that was causing the same, Exception before exception
I searched a lot, in short, it is how i solve it
In my case, changing gridDelegate, my exception is fixed!
Note: Don't use any Mathematical operation like (/,*) that may produce a double value!!!
Give a like if you find it Useful! :)
Upvotes: 3
Reputation: 6418
Pretty sure you've sorted it out already but as I don't see any accepted answer I'll leave mine to be useful to others.
I was in the same situation and I had the same problem, dough in the cross axis.
The problem is that you're providing both mainAxisSpacing: 30.0
and crossAxisSpacing: 20.0
values.
With a crossAxisCount: 1
you're not using the main axis so mainAxisSpacing
throws the infamous SliverGeometry is not valid: The "scrollExtent" is negative.
error.
Independently from your scrollingDirection
you should only pass the crossAxisSpacing
value.
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
childAspectRatio: 4 / 3,
crossAxisSpacing: 20.0),
padding: EdgeInsets.only(left: 20),
scrollDirection: Axis.horizontal,
itemCount: products.length,
itemBuilder: (context, i) => ChangeNotifierProvider.value(
value: products[i],
child: Consumer<Product>(
builder: (context, product, _) {
return ProductCard(
product: product,
onSelected: (prod) {
setState(() {
products.forEach(
(item) {
item.isSelected = false;
},
);
prod.isSelected = true;
});
Upvotes: 5
Reputation: 624
I found the issue,
I was using empty list in gridview builder
List<int> myList = [];
GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisSpacing: Dimensions.PADDING_SIZE_LARGE,
mainAxisSpacing: ResponsiveHelper.isDesktop(context)
? Dimensions.PADDING_SIZE_LARGE
: 0.01,
childAspectRatio:
ResponsiveHelper.isDesktop(context) ? 4 : 4,
crossAxisCount:
ResponsiveHelper.isMobile(context) ? 1 : 2,
),
physics: BouncingScrollPhysics(),
shrinkWrap: true,
itemCount: myList.length,
itemBuilder: (context, index) {
return Text("");
},
)
myList was not having any values
Upvotes: 0