How dispose Global key from provider with flutter web?
You do not need to manually dispose of a GlobalKey
. The main requirement is that they cannot be inserted into the widget tree twice. This is not the case with other keys (LocalKey
s):
// this is allowed
Row(
children: [
SizedBox(key: Key('hello')),
Container(key: Key('hello')),
],
)
// this is not
final key = GlobalKey<ScaffoldState>();
Row(
children: [
Scaffold(key: key),
Scaffold(key: key),
],
)
A common reason why this is violated is animations. While the animation between one page and another page is playing, both pages are in the widget tree, and if they have the same GlobalKey
, an error will be thrown.
Calling globalKey.currentState!.dispose()
actually disposes the State
of the associated widget. You should not call this yourself.
Instead, provide a new GlobalKey
to the second subtree or remove the old one before navigating to the new page.