Error: SliverGeometry is not valid: The "scrollExtent" is negative. in flutter Sliver Grid View Laout

Viewed 4342

EXCEPTION CAUGHT BY RENDERING LIBRARY

The following assertion was thrown during performLayout():
SliverGeometry is not valid: The "scrollExtent" is negative.
The RenderSliver that returned the offending geometry was: RenderSliverGrid#f9778 relayoutBoundary=up2 NEEDS-LAYOUT NEEDS-PAINT:
  creator: SliverGrid ← MediaQuery ← SliverPadding ← Viewport ← IgnorePointer-[GlobalKey#15db4] ←
    Semantics ← _PointerListener ← Listener ← _GestureSemantics ←
    RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#30301] ← _PointerListener ← Listener
    ← ⋯
  parentData: paintOffset=Offset(0.0, 0.0) (can use size)
  constraints: SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.idle,
4 Answers

I found I was getting the 'scrollExtent is negative' error when I passed 0 items to the GridView.builder. This was occurring prior to my data being loaded.

In my build function, I check to see if the List of items is zero, and if so, I render an empty Container() instead of the Gridview. This stopped the error from happening.

I believe this is a bug with the GridView, as it should be able to gracefully handle no records.

Not sure what your code looks like, but I was using NotificationListener on my GridView.builder.

I got this error when I cleared the contents of my Gridview. (In my code, I have a search filter, which clears the search results on save, then calls the search function). Once I added a simple flag to show a progress indicating while loading and a message if the results are 0, the error went away.

for me, that is the old code causing exception: old code

then I began searching and tried some changes. At the point I found gridDelegate was causing Exception.

then after some changes: new code

I got my Exception fixed.

Note: Don't use any Mathematical operation that may produce a double value!

I hope that is helpful for you

Related