mtkguy
mtkguy

Reputation: 275

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});
            },
          );
        },
      ),
    );

Upvotes: 3

Views: 2153

Answers (1)

Ahmed AL-Yousif
Ahmed AL-Yousif

Reputation: 460

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});
        },
      );

Upvotes: 1

Related Questions