I have a CustomScrollView that receives a future sliver, it works but the sliver keeps on repeating its elements
Here is my code in homepage
late Future<List<DatabaseContent>> europe;
@override
void initState() {
super.initState();
europe = DatabaseHelper.instance.getDatabaseContent();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: CustomScrollView(
slivers: [
FutureBuilder(
future: europe,
builder: (context, snapshot) {
if (snapshot.hasData) {
return EuropeGroup(data: snapshot.data!);
} else {
return const LoadingSliver();
}
},
)
],
));
}
The data is passed to this widget, it displays everything correctly but keeps repeating the elements indefinitely
class EuropeGroup extends StatefulWidget {
final List<DatabaseContent> data;
const EuropeGroup({super.key, required this.data});
@override
State<EuropeGroup> createState() => _EuropeGroupState();
}
class _EuropeGroupState extends State<EuropeGroup> {
SliverGridDelegate gridDelegate =
const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2);
int _childCount = 0;
@override
Widget build(BuildContext context) {
return SliverGrid(
delegate: SliverChildBuilderDelegate(
childCount: widget.data.length,
(context, index) {
DatabaseContent _current = widget.data[index];
return Card(
child: Text("${_current.last_update} "
"${_current.geo} "
"${_current.time_period} "
"${_current.obs_value} "
"${_current.obs_flag} "),
);
},
),
gridDelegate: gridDelegate);
}
}
I tried searching google but did not found relevant info why this is happening
Use initstate to initialize the variable
void initState() {
super.initState();
_childCount = widget.data.length;
}
and them use in widget
SliverChildBuilderDelegate(
childCount: _childCount,