didPushRoute method

  1. @override
Future<bool> didPushRoute(
  1. String route
)
override

Called when the host tells the application to push a new route onto the navigator.

Observers are expected to return true if they were able to handle the notification. Observers are notified in registration order until one returns true.

This method exposes the pushRoute notification from SystemChannels.navigation.

Implementation

@override
Future<bool> didPushRoute(String route) {
  log.info("platform:didPushRoute: $route");
  // Look backwards in history for a match, then pop routes to match.
  var matches = state.matchHistory(route);
  if (matches.isNotEmpty) {
    // Pop the route
    for (var match in matches) {
      if (match.currTab != null) {
        if (match.currTab != state.currTab) {
          log.info(
              "platform:didPushRouteInformation > move tab ${match.currTab}");
          state.currTab = match.currTab;
        } else {
          var tabNavState =
              state.tabState[match.currTab!].navigatorKey.currentState!;
          if (tabNavState.canPop() == true) {
            tabNavState.popUntil((route) {
              var isPath = route.path == route;
              if (!isPath) {
                log.info(
                    "platform:didPushRouteInformation ^ popped ${match.route}");
              }
              return isPath;
            });
          } else {
            log.warning(
                "platform:didPushRouteInformation ! unable to pop ${match.currTab}");
          }
        }
      } else {
        //pop root?
        log.warning("platform:didPushRouteInformation can't pop root (TBD)");
      }
    }

    return SynchronousFuture(true);
  } else {
    /// How do we know where to push this route?

    log.warning(() {
      var str = "";
      str +=
          'platform:didPushRouteInformation No found routes for ${route}\n';
      str += '  in ->\n';
      if (state.history.isEmpty) {
        str += '    - no history\n';
      } else {
        str +=
            '    - ${state.history.map((element) => element.route).join('\n    - ')}';
      }
      return str;
    });
  }
  return super.didPushRoute(route);
}