ScrollExtent is negative in flutter

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. home page view with products


It is my code that was causing the same, Exception before exception

I searched a lot, in short, it is how i solve it

What i did to fix it

In my case, changing gridDelegate, my exception is fixed!

Note: Don't use any Mathematical operation like (/,*) that may produce a double value!!!

Please give a like if it is Useful for you! :)


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;
                });