DavideBelsole / great_list_view

pub.dev library for flutter
MIT License
41 stars 22 forks source link

Changing an attribute of an item not working #17

Closed mregnauld closed 2 years ago

mregnauld commented 2 years ago

After reading your ReadMe, I tried the Example 5. In that example, there is the following, which works fine:

void swapList() {
    setState(() {
      if (dispatcher.currentList == listA) {
        dispatcher.dispatchNewList(listB);
      } else {
        dispatcher.dispatchNewList(listA);
      }
    });
  }

OK, now let's say that I want to just change the value of the color of one item in the list, so here is what I tried:

void swapList() {
    setState(() {
      dispatcher.currentList[0] = ItemData(1, Colors.black);
      dispatcher.dispatchNewList(dispatcher.currentList);
    });
  }

But for some reason, nothing happens: the first item of the list isn't refreshed.

After some trials and errors, I found out that I have to duplicate the list and create a new instance for the first item, like the following, to make it work:

void swapList() {
    setState(() {
      List<ItemData> listAbis = [...dispatcher.currentList];
      listAbis[0] = ItemData(1, Colors.black);
      dispatcher.dispatchNewList(listAbis);
    });
  }

But why? Is this the expected behavior?

tRuNKator commented 2 years ago

Yes, it's expected behaviour. diffutils calculates difference between two lists, if you modify original list - nothing happens

mregnauld commented 2 years ago

Oh ok, it took me a while to get it, as my app had some weird behavior, but it makes sense now. Thanks.

tRuNKator commented 2 years ago

Oh ok, it took me a while to get it, as my app had some weird behavior, but it makes sense now. Thanks.

Anyway, you can dispatch update thought AnimatedListController.notifyItemChanged

mregnauld commented 2 years ago

ok, definitely good to know, thanks!