Flutter: A dismissed Dismissible widget is still part of the tree

Updated: February 15, 2023 By: Augustus 11 comments

This article shows you how to solve a problem when working with the Dismissible widget in Flutter.

The Problem

When working with the Dismissible widget in Flutter, you may fall into this error:

A dismissed Dismissible widget is still part of the tree.
Make sure to implement the onDismissed handler and to immediately remove the Dismissible widget from
the application once that handler has fired.

Why does it happen?

The reason is that the value passed to the key property isn’t unique or related to the index of each item in a list. These two settings get me in trouble and you should get rid of them:

key: Key(index),
key: ValueKey(index),

After removing an item from a list, the list will shift the positions of the items and the Dismissible widget cannot recognize the removal of the item.

Solution

The solution is quite simple and all you need to do is set the key property to UniqueKey(), like this:

key: UniqueKey(),

That’s it. Hope this helps.

Further reading:

You can also check out our Flutter category page or Dart category page for the latest tutorials and examples.

Subscribe
Notify of
guest
11 Comments
Inline Feedbacks
View all comments
Juan
Juan
5 months ago

This has been giving me a sleepless night. Thank you for helping.

FaizDaoud
FaizDaoud
1 year ago

Thankyou 🙂

Lud
Lud
1 year ago

Thankyou bro

notme
notme
2 years ago

awesome!

Muhammad Ali
Muhammad Ali
2 years ago

thankyouu.

zami
zami
2 years ago

thanks a lot

James
James
2 years ago

thank you

Arturo
Arturo
2 years ago

Thank you, but now there is another issue, if I dismiss two items too fast, only one of them disappears, and the other reappears, do you know why it happens?

umair
umair
2 years ago

it was quite helpful thanks a lot

Bright Mukonesi
Bright Mukonesi
3 years ago

I thought i was the only one using the laziness shortcut to create a key. Lol

Albert Eije
Albert Eije
3 years ago

Very good. Thanks a lot!

Related Articles