sr2k23
sr2k23

Reputation: 137

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

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,

Upvotes: 7

Views: 6794

Answers (4)

Satyam
Satyam

Reputation: 446

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

Upvotes: 1

Matt Ireland
Matt Ireland

Reputation: 261

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.

Upvotes: 19

MattyO
MattyO

Reputation: 125

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.

Upvotes: 1

Akif
Akif

Reputation: 7650

There is a related issue here. Still unresolved.

ScrollExtent is negative in flutter

Btw, you can check on Github.

https://github.com/flutter/flutter/issues/39963

And for the reproduce of the error: https://dartpad.dev/2630a061cf2632a1ffd6fb6c13c94e4c

Upvotes: 2

Related Questions