I have a StatefulWidget that adds to its Navigator's local history via ModalRoute.of(context).addLocalHistoryEntry()
. When certain changes occur, I want to reset the widget's state and clear any local history that it added. I don't see any API on ModalRoute
to accomplish this.
Asked
Active
Viewed 753 times
0

Jacob Phillips
- 8,841
- 3
- 51
- 66
1 Answers
0
What worked for me is to check the current route before the first build and then use Navigator.popUntil()
to remove all local history entries at once.
@override
void didChangeDependencies() {
initialRoute ??= ModalRoute.of(context);
super.didChangeDependencies();
}
void reset() {
Navigator.of(context).popUntil((route) => route == initialRoute);
}
Note however if another widget uses local history entries on the same ModalRoute, those local history entries will also be removed. A more thorough solution would require access to the topmost route or at least the length of local history entries on the current ModalRoute.

Jacob Phillips
- 8,841
- 3
- 51
- 66