mushroom
mushroom

Reputation: 1945

Preserving state across pages in Flutter

I have the following widget tree on two pages A and B.

Hero
|-SomeStatelessWidget
  |-SomeStatefulWidget

Tapping on the widget triggers navigation from page A to page B. However, state for SomeStatefulWidget is recreated after the transition. How can I preserve the state of SomeStatefulWidget across page navigation?

Upvotes: 1

Views: 613

Answers (1)

pr0gramista
pr0gramista

Reputation: 9008

You can use GlobalKey to preserve the same state across different pages.

class RandomColor extends StatefulWidget {
  RandomColor({Key key}) : super(key: key); // need this

  @override
  State<StatefulWidget> createState() => RandomColorState();
}

Here you have a great Google Developers video about keys.

You may also consider using some kind of state managment.

Upvotes: 1

Related Questions