-1

I have the following below SliverGrid which is being populated from an API. I don't know the number number of items until I have made the API call. The current implementation below render the text 'No More' once I scroll past the past items because I have not set the childCount. Is there a way for me to set childCount . I would like for it to stop scrolling and rendering anything.

I have tried creating a variable to hold the childCount variable and reseting that inside the snapshot.hasData block, however the new value seems to ignored.

return SliverGrid(
  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4),
  delegate: SliverChildBuilderDelegate(
        (BuildContext context,int index)
        {
          return StreamBuilder(
            stream: bloc.attributesStream ,
            builder: (context, snapshot)
            {                        
                  if(snapshot.hasData)
                  {
                      return HomeIconCell(snapshot.data[index]);
                      //want to set childCount to snapshot.data.length

                  }
                  return Text("No more");

            },
            );               
        },
        childCount: gridChildCount
));

}

yla-dev
  • 144
  • 2
  • 10
  • see [childCount](https://api.flutter.dev/flutter/widgets/SliverChildBuilderDelegate/childCount.html) property documentation – pskink Aug 26 '19 at 03:58
  • Returning null from the builder method as the document suggests doesn't work since I am using a StreamBuilder – yla-dev Aug 27 '19 at 23:52
  • it does not matter if you use `StreamBuilder` or not – pskink Aug 28 '19 at 04:30

2 Answers2

1

If this is a StatefulWidget you can use setState to update the grid count.

...
                builder: (context, snapshot)
                {                        
                      if(snapshot.hasData)
                      {
                          return HomeIconCell(snapshot.data[index]);
                          //want to set childCount to snapshot.data.length
                          setState((){ gridChildCount = snapshot.data.length; });
                      }
...
Nico Spencer
  • 940
  • 6
  • 11
0

I ended by solving the problem by wrapping SliverGrid inside the StreamBuilder

Widget build(BuildContext context) 
  {   
    final bloc = AttributesProvider.of(context);
    bloc.getAttributeList();
   // APIServiceProvider  api = APIServiceProvider();
    //Future<List<AttributeModel>> attributesListFuture = api.getAttributes();

    return StreamBuilder(
      stream: bloc.attributesStream,
      builder: (context, AsyncSnapshot<List<AttributeModel>>snapshot) =>SliverGrid(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4),
                delegate: SliverChildBuilderDelegate(
                (context, index) => HomeIconCell(snapshot.data[index]),
                childCount: snapshot.hasData ? snapshot.data.length : 0,
             )
      )
      );
  }
yla-dev
  • 144
  • 2
  • 10