How to pass data
inside buildListItem
function as a parameter?
When I add a third parameter data
to buildListItem
, it gives an error in the IDE:
The argument type ‘Widget’ can’t be assigned to the parameter type
‘Widget? Function(BuildContext, int)’.
final List<RapportData> data = snapshot.data!;
return ListView.separated(
physics: const ScrollPhysics(),
itemCount: data.length,
itemBuilder: buildListItem, // how to pass `data`?
separatorBuilder: (context, index) => const Divider(),
);
Widget buildListItem(BuildContext context, int index) {
return GestureDetector(
onTap: () {
var rapportData = data[index]; //data is not avalable here
onTapFunction(context, Group(rapportData));
},
child: ListTile(...
...
}
2
Answers
Certainly! You can achieve that by using an anonymous function directly inside the
itemBuilder
parameter. Here’s how you can do it:This way, you create an anonymous function directly inside the
itemBuilder
parameter, allowing you to access thedata
list within it.You can try pass parameter like this: