navigateTo method

Future navigateTo(
  1. BuildContext context,
  2. String path, {
  3. bool replace = false,
  4. bool clearStack = false,
  5. bool maintainState = true,
  6. bool rootNavigator = false,
  7. TransitionType? transition,
  8. Duration? transitionDuration,
  9. RouteTransitionsBuilder? transitionBuilder,
  10. RouteSettings? routeSettings,
  11. bool? opaque,
})

Similar to Navigator.push but with a few extra features.

Implementation

Future navigateTo(
  BuildContext context,
  String path, {
  bool replace = false,
  bool clearStack = false,
  bool maintainState = true,
  bool rootNavigator = false,
  TransitionType? transition,
  Duration? transitionDuration,
  RouteTransitionsBuilder? transitionBuilder,
  RouteSettings? routeSettings,
  bool? opaque,
}) {
  RouteMatch routeMatch = matchRoute(
    context,
    path,
    transitionType: transition,
    transitionsBuilder: transitionBuilder,
    transitionDuration: transitionDuration,
    maintainState: maintainState,
    routeSettings: routeSettings,
    opaque: opaque,
  );

  Route<dynamic>? route = routeMatch.route;
  Completer completer = Completer();
  Future future = completer.future;

  if (routeMatch.matchType == RouteMatchType.nonVisual) {
    completer.complete("Non visual route type.");
  } else {
    if (route == null && notFoundHandler != null) {
      route = _notFoundRoute(context, path, maintainState: maintainState);
    }

    if (route != null) {
      final navigator = Navigator.of(context, rootNavigator: rootNavigator);
      if (clearStack) {
        future = navigator.pushAndRemoveUntil(route, (check) => false);
      } else {
        future = replace
            ? navigator.pushReplacement(route)
            : navigator.push(route);
      }
      completer.complete();
    } else {
      final error = "No registered route was found to handle '$path'.";
      print(error);
      completer.completeError(RouteNotFoundException(error, path));
    }
  }

  return future;
}