When is the State object destroyed in a StatefulWidget?
Asked Answered
A

2

5

One of Flutter's mantras is that widgets are immutable and liable to be rebuilt at a moment's notice. One reason for the StatefulWidget is the accompanying State object, which 'hangs around' beyond any individual build() method call. This way text values, checkbox selections, can persist when the widgets themselves are being rebuilt.

However, when are the State objects themselves destroyed? Is it when their associated widget is removed from the widget tree? And under what circumstances does that happen exactly-- when a Navigator is used to go to a new widget? When you go to a different entry in a TabBar?

It's a little hazy to me, the scenarios in which widgets are actually removed from the widget tree and their associated state destroyed. What other circumstances do I need to be aware of that my State obejct is liable to vanish, so I can take appropriate measures with PageStorageKeys and whatnot?

Arthritis answered 1/8, 2019 at 5:32 Comment(2)
"However, when are the State objects themselves destroyed?" - hereBriton
This might be an updated link for the above.Cashmere
T
9

The general answer is: When it's associated Element (the BuildContext object) is disposed after having been removed from the Element tree.

Note that an Element (and therefore a Widget) cannot remove itself from the tree. It has to be its parent that removes it.

Most of the time, this happens depending on what the build method of its parent do.

There are two main scenarios:

  • The build method returned a different widget tree. Typically going from:
return Foo();

To:

return Bar();

will destroy the state of Foo.

Note that this also happens when Foo "moved" :

return Foo();

To:

return Bar(child: Foo());

will still dispose the state of Foo.

  • The second scenario is when the Key change:
return Foo();

into:

return Foo(key: Key("foo")) ;

Or:

return Foo(key: Key("bar"));

into:

return Foo(key: Key("foo")) ;

Will both destroy the state of the previously created Foo.

Trappings answered 1/8, 2019 at 7:46 Comment(1)
Thanks Rémi. I assume that if you're using a Navigator to move between widgets-- say, if you've filled out some info in a form, then navigate away, then pop back, the form info will be gone, since (I assume) the form will have been removed from the widget tree?Arthritis
D
0

on Dispose Methode

@override
dispose()
Disposal answered 1/8, 2019 at 8:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.