Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass data inside itemBuilder when building ListView?

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)’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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(...
    
    ... 
}

>Solution :

You can try pass parameter like this:

final List<RapportData> data = snapshot.data!;

return ListView.separated(
  physics: const ScrollPhysics(),
  itemCount: data.length,
  itemBuilder: (context, index) => buildListItem(context, data[index]),
  separatorBuilder: (context, index) => const Divider(),
);

Widget buildListItem(BuildContext context, RapportData rapportData) {
  return GestureDetector(
    onTap: () {
      //rapportData can use here
      onTapFunction(context, Group(rapportData));
    },
    child: ListTile(...
    
    ... 
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading