didPopRoute method

  1. @override
Future<bool> didPopRoute()
override

Called when the system tells the app to pop the current route, such as after a system back button press or back gesture.

Observers are notified in registration order until one returns true. If none return true, the application quits.

Observers are expected to return true if they were able to handle the notification, for example by closing an active dialog box, and false otherwise. The WidgetsApp widget uses this mechanism to notify the Navigator widget that it should pop its current route if possible.

This method exposes the popRoute notification from SystemChannels.navigation.

Handling backs ahead of time

Not all system backs will result in a call to this method. Some are handled entirely by the system without informing the Flutter framework.

Android API 33+ introduced a feature called predictive back, which allows the user to peek behind the current app or route during a back gesture and then decide to cancel or commit the back. Flutter enables or disables this feature ahead of time, before a back gesture occurs, and back gestures that trigger predictive back are handled entirely by the system and do not trigger this method here in the framework.

By default, the framework communicates when it would like to handle system back gestures using SystemNavigator.setFrameworkHandlesBack in WidgetsApp. This is done automatically based on the status of the Navigator stack and the state of any PopScope widgets present. Developers can manually set this by calling the method directly or by using NavigationNotification.

Implementation

@override
Future<bool> didPopRoute() async {
  assert(mounted);
  if (_navigator == null) return false;
  return await _navigator!.maybePop();
}