Listview.Uilder does not update when removing an item from the list, but when adding yes

Asked

Viewed 445 times

0

I have a Future Builder that takes a list, and then returns a column with a Listview.Builder() and a button below, when using the button to add an element the list updates successfully, now when using a button that is inside Listtile to delete the element, the element is deleted but the list does not update. Follow an example of what it would be.

Dialog(
 FutureBuilder(
  future: getChapters(),
  builder (context, snapshot) {
    return Column(
      children: <Widget>[
        Container(
          child: ListView.builder(
            itemCount:snapshot.data.length,
            builder: (context, index) {
              return ListTile(
                title: Text("teste");
                trailing: InkWell(
                  child: Icon(Icons.delete),
                  onTap: (){
                    deleteChapter();
                    setState((){});
                  }
                ),
              );
            }
          )
        ),
        RaisedButton() {   
          onPressed: () {
            addChapter();
            setState((){});   
          }
        }
      ]
    );
  }
 )
)

When deleting an item from the list, it only updates by closing and opening the widget.

  • It would be good to analyze a complete code in this case, if we do not need to keep guessing what 'deleteChapter()' does, the 'Futurebuilder' is inside what, etc..

  • Yes, I also think, it is very big, I agreed to this problem, but it was not solved.

  • Oh that is, the ideal would be to create a new project and 'replicate' only the error that you are with doubt of the behavior, then share this code. The entire project ends up making it difficult to analyze unnecessary things rather than focusing on the error. I’ve done it a lot and most of the time I find the problem myself trying to isolate the mistake in another project.

  • Dude, a tip is when you’re using setState() put inside it the method you want to execute, not outside, following your example do: setState((){deleteChapter();});

  • Inform what exists within the method deleteChapter() that we can help you better.

  • I edited the example, I’m new to the questions so I didn’t post the well specified problem. deleteChapter() changes the list in the same way as addChapter(), of course, one adds an element and the other removes, and then the database is updated with the new list. The button you add is outside Futurebuilder() and it successfully updates the Dialog() after adding, now the button that is in Listtile() removes an element from the list, but you can only see the change by closing and opening the Dialog().

  • In a nutshell, the add and remove processes are nearly the same, but when removing an element from the list using a button inside the listTile the screen does not update, but it successfully alters the list.

  • @Nelsonthiago Edit your question and add the code inside your method deleteChapter() and addChapter() then we can better understand the situation.

  • I edited the post. really the problem was due to deleteChapter() being async

  • @Nelsonthiago as it is your first time here and as our help was not so well accepted by you to improve your question, take a little time to read this here:How to create a Minimum, Complete and Verifiable example... This will help us help you in the near future.

  • 1

    Actually I just didn’t improve the question why I was running out of time, if I had improved you probably would have found the problem well before I.

  • 1

    @Nelsonthiago If you managed to solve the problem, the solution should be published in the answer area, not as an edition in the question. The question should only be related to the question itself. Read Answer your own question. I already reversed the edit you made by adding the answer to the question.

Show 7 more comments

1 answer

0

Add a Uniquekey to your Listview that should work. When the flutter is rebuilding its Widget it makes the comparison by type to know if the widget should be rebuilt or not, as it had a Listview before and after the same Listview in the same position flutter thinks should not rebuild this guy (even though their data has been modified), so to help the flutter in this work you add a Uniquekey that will be different all your life.

  • As it is only including and removing items from the list, it is not necessary to use Keys, This could even cost a little more processing. And if possible when answering the question, put some practical example, such as where by this UniqueKey.

  • I don’t understand why you’re saying it’s not necessary. It’s one of the main uses of Keys...

  • I agree that it is one of the main uses, I just say that it is not necessary to use it in this context. If it is going to do a scheme to delete and restore the ai item yes it would be necessary to use, but as it is only including/deleting it is not necessary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.