Why is sliverlist not working in my customscrollview?

Issue

I am trying to add a sliverlist but it does not work for me, I am very new to using flutter and I am confused.

@override
Widget build(BuildContext context) {
return Scaffold(
  body: const CustomScrollView(
    slivers: <Widget>[
      SliverAppBar(
        title: Text('Sample Slivers'),
        leading: Icon(Icons.menu),
        backgroundColor: Colors.orangeAccent,
        expandedHeight: 90.0,
        floating: true,
        pinned: true,
        snap: false,
      ),
      SliverList(
        delegate: SliverChildBuilderDelegate((context) {}),
      )
    ],
  ),
  bottomNavigationBar: _navigatorAppBar(),
  floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  floatingActionButton: _buildFab(),
);
}
}

img

Solution

  1. Remove the const keyword of the CustomScrollView.

  2. Replace SliverChildBuilderDelegate((context) {}) with:

SliverChildBuilderDelegate((context, index) {})

Full code:

@override
Widget build(BuildContext context) {
return Scaffold(
  body: CustomScrollView(
    slivers: <Widget>[
      SliverAppBar(
        title: Text('Sample Slivers'),
        leading: Icon(Icons.menu),
        backgroundColor: Colors.orangeAccent,
        expandedHeight: 90.0,
        floating: true,
        pinned: true,
        snap: false,
      ),
      SliverList(
        delegate: SliverChildBuilderDelegate((context, index) {}),
      )
    ],
  ),
  bottomNavigationBar: _navigatorAppBar(),
  floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  floatingActionButton: _buildFab(),
);
}

Answered By – Mobina

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *