Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listview builder creating duplicate of list item

I have a list of widgets built using a listview builder. Problem is when i change the state of an item in the list(using the ontap property of the list item) the state changes quite alright but an exact replica of that mutated list item is added at the very top of my list

Snippet

List data = [1,2,2,1,1];

return Scrollbar(
      child: ListView.builder(

        itemCount: data.length,
        itemBuilder: (context, int index) {
          final s = data[index];

          return ListTile(

            dense: false,
            leading: leading(leading),
            title: Text(s),
            subtitle: Text(
              "${s}",
              style: Theme.of(context).textTheme.caption,
            ),
            onTap: () {
              setState(() {//do something to the subtitle});
            },
          );
        },
      ),
    );
like image 672
mtkguy Avatar asked Dec 27 '25 15:12

mtkguy


1 Answers

Try giving everyListTile a unique Key

For example:

     return ListTile(
        key: Key("${index}"),
        dense: false,
        leading: leading(leading),
        title: Text(s),
        subtitle: Text(
          "${s}",
          style: Theme.of(context).textTheme.caption,
        ),
        onTap: () {
          setState(() {//do something to the subtitle});
        },
      );
like image 57
Ahmed AL-Yousif Avatar answered Dec 31 '25 17:12

Ahmed AL-Yousif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!