RouteObserver<R extends Route> class

A Navigator observer that notifies RouteAwares of changes to the state of their Route.

RouteObserver informs subscribers whenever a route of type R is pushed on top of their own route of type R or popped from it. This is for example useful to keep track of page transitions, e.g. a RouteObserver<PageRoute> will inform subscribed RouteAwares whenever the user navigates away from the current page route to another page route.

To be informed about route changes of any type, consider instantiating a RouteObserver<Route>.

Type arguments

When using more aggressive lints, in particular lints such as always_specify_types, the Dart analyzer will require that certain types be given with their type arguments. Since the Route class and its subclasses have a type argument, this includes the arguments passed to this class. Consider using dynamic to specify the entire class of routes rather than only specific subtypes. For example, to watch for all ModalRoute variants, the RouteObserver<ModalRoute<dynamic>> type may be used.

To make a StatefulWidget aware of its current Route state, implement RouteAware in its State and subscribe it to a RouteObserver:
link
// Register the RouteObserver as a navigation observer.
final RouteObserver<ModalRoute<void>> routeObserver = RouteObserver<ModalRoute<void>>();

void main() {
  runApp(MaterialApp(
    home: Container(),
    navigatorObservers: <RouteObserver<ModalRoute<void>>>[ routeObserver ],
  ));
}

class RouteAwareWidget extends StatefulWidget {
  const RouteAwareWidget({super.key});

  @override
  State<RouteAwareWidget> createState() => RouteAwareWidgetState();
}

// Implement RouteAware in a widget's state and subscribe it to the RouteObserver.
class RouteAwareWidgetState extends State<RouteAwareWidget> with RouteAware {

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    routeObserver.subscribe(this, ModalRoute.of(context)!);
  }

  @override
  void dispose() {
    routeObserver.unsubscribe(this);
    super.dispose();
  }

  @override
  void didPush() {
    // Route was pushed onto navigator and is now topmost route.
  }

  @override
  void didPopNext() {
    // Covering route was popped off the navigator.
  }

  @override
  Widget build(BuildContext context) => Container();

}

Inheritance

Constructors

RouteObserver()

Properties

hashCode int
The hash code for this object.
no setterinherited
The navigator that the observer is observing, if any.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

debugObservingRoute(R route) bool
Whether this observer is managing changes for the specified route.
didPop(Route route, Route? previousRoute) → void
The Navigator popped route.
override
didPush(Route route, Route? previousRoute) → void
The Navigator pushed route.
override
didRemove(Route route, Route? previousRoute) → void
The Navigator removed route.
inherited
didReplace({Route? newRoute, Route? oldRoute}) → void
The Navigator replaced oldRoute with newRoute.
inherited
didStartUserGesture(Route route, Route? previousRoute) → void
The Navigator's routes are being moved by a user gesture.
inherited
didStopUserGesture() → void
User gesture is no longer controlling the Navigator.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
subscribe(RouteAware routeAware, R route) → void
Subscribe routeAware to be informed about changes to route.
toString() String
A string representation of this object.
inherited
unsubscribe(RouteAware routeAware) → void
Unsubscribe routeAware.

Operators

operator ==(Object other) bool
The equality operator.
inherited