Trigger events on page navigation

Flutter tips Published on

Need to know exactly when a user moves from one page to another in your Flutter app? Maybe you want to log which pages they visit, update some state, or show a special message.

Flutter provides a handy tool for this called RouteObserver. It lets you listen to what's happening with the pages (routes) in your app's navigator.

Using RouteObserver

To use RouteObserver, you need to create your own class that extends it. This new class will listen for navigation changes.

Create Your Observer Class

In this class, you can override methods like didPush, didPop, and didReplace to react to different navigation actions.

class MyNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
  
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    // This runs when a new page is opened
    print('Navigated to: ${route.settings.name}');
  }

  
  void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
    // This runs when a page is replaced by another
     print('Page replaced with: ${newRoute?.settings.name}');
  }

  
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    // This runs when you go back from a page
    print('Returned from: ${route.settings.name}');
  }
}

Add the Observer to Your App

Next, you need to tell your MaterialApp (or CupertinoApp) to use your new observer. You do this by adding an instance of your observer to the navigatorObservers list.

class MyApp extends StatelessWidget {
  // Create an instance of your observer
  final navObserver = MyNavigatorObserver();

  
  Widget build(BuildContext context) {
    return MaterialApp(
      // Provide your observer to the app
      navigatorObservers: [navObserver],
      // ... other app setup ...
    );
  }
}

Now, whenever a page is pushed, popped, or replaced in your app's main navigator, the corresponding method in your MyNavigatorObserver class will run. This lets you run specific code right when these navigation events happen.

Key Navigation Events

Here are the main events you can listen for:

didPush

Happens when you navigate to a new page (like using Navigator.push).

didPop

Happens when you go back from a page (like using Navigator.pop).

didReplace

Happens when one page is swapped for another without adding to the stack (like using Navigator.pushReplacement).

Using RouteObserver is a simple and powerful way to manage actions based on page changes in your Flutter applications.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is RouteObserver in Flutter?

RouteObserver is a class in Flutter that lets you listen to navigation events, such as when pages are added, removed, or replaced in the navigator stack.

Why would I use RouteObserver?

You might use it to track user navigation for analytics, perform actions when a specific page becomes visible or hidden, or manage resources based on the current page.

How do I add a RouteObserver to my app?

You create a custom class that extends `RouteObserver` and then provide an instance of this class to the `navigatorObservers` list property of your `MaterialApp` or `CupertinoApp`.

What navigation events can I track?

You can track `didPush` (new page added), `didPop` (page removed by going back), and `didReplace` (one page swapped for another).

Read more
You may also be interested in
Smart asking for rating  blog card image
Smart asking for rating
Published on 2025-04-28
Replace type code with Union type  blog card image
Replace type code with Union type
Published on 2025-05-12T09:03:17.217Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved