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(),
);
}
}
Solution
-
Remove the
const
keyword of theCustomScrollView
. -
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)