Search code examples
listflutterdartwidgetinstance

Flutter: Pass instance of list through widget tree -> is not changing the values of instances in list


I´ve a Homescreen with a list of items (List<book> listOfBooks). In a child widget I've a gridView creating Items of the widget BookItem().

My problem is when I'm passing BookItem() the parameter book, which is book itself and changing it in the widget BookItem it doesn't change the values of book in listOfBooks.

I hope that wasn´t to confusing. My question is: Is there being created a new instance not related to the listOfBooks or have I made any other mistakes?


Solution

  • If you take the book of listBooks like that:

    listBooks.map((book){
      return itemBook(book);
    }).toList();
    

    And into itemBook try to change the book, you need to update the item into listBook. This update the list correctly:

    listBooks[indexBookSelected] = bookUpdated;
    

    This doesnt work:

    book = bookUpdated;
    

    Of course after change the book need to call setState to update the view.