Be aware of your routes with RouteAware — Flutter!

There can be cases when you wish to perform certain actions on the change navigation history. This is where RouteAware comes to the rescue!

  • didPop()
    When we pop the current screen, didPop method is called.
  • didPopNext()
    If you have extended Page1 with RouteAware, and if Page2 is popped so that Page1 is visible now, didPopNext is called. In other words, this method is called when the top screen is popped off and the current screen is visible.
  • didPush()
    This is called when the current screen or route has been pushed into the navigation stack!
  • didPushNext()
    When we push Page2 from Page1, didPushNext is called. In other words, this method is called when a new screen/route is pushed from the current screen and the current screen is no longer visible.
final RouteObserver<ModalRoute> routeObserver = RouteObserver<ModalRoute>();
@override
void initState() {
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
routeObserver.subscribe(this, ModalRoute.of(context)!);
});
super.initState();
}

Don’t forget to connect with me!

Don’t stop, until you are breathing!💙
- Abhishek Doshi

--

--

Google Developer Expert — Dart, Flutter & Firebase 💙💛

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store