I am facing the following problem:
I have a StatefulWidget which can generate different view in case of state change:
class _HomePageState extends State<HomePage> {
bool _changed = false;
@override
Widget build(BuildContext context) {
return changed ? View2() : View1();
}
}
If I change a state _changed
to true
and hit the back button, a then the forward button, I would like my application to create fresh _HomePageState
and display a View1
, but currently the View2
is presented, so I believe that the state is persisted somehow.
How can I force rebuild of the widget’s state?
Example app presenting the problem: link
Steps to reproduce:
- Open some site, eg. www.google.com
- Paste the link to the app https://issueapp.tiiny.site
- Change the state of the app by pressing a button
- Press browser’s back button
- Press browser’s forward button
GIF presenting the issue:
2
Answers
This only happen on release version of flutter web. Your flutter web state will be keep in navigation stack, until you do a page refresh.
Here some additional comment from flutter web issue related to Refreshing browser resets navigation stack and local variables :
Read the github complete comment here by mdebbar
There are a couple of ways to do that, the easiest way now is just adding a wrapper as a parent widget, that receives a callback when you press the back button from your browser, and then restart the widget.
Results:
Code:
MyParentWidget
first.