replaceTo<T, TO> method

Future<T?> replaceTo<T, TO>(
  1. Widget page, {
  2. bool safe = false,
  3. PageTransitionType? type,
})

Replaces the current route with a new page.

If safe is enabled, the replacement is skipped when navigating to the same route.

Implementation

Future<T?> replaceTo<T, TO>(
  Widget page, {
  bool safe = false,
  PageTransitionType? type,
}) async {
  final currentRoute = ModalRoute.settingsOf(this)?.name;
  final newRoute = page.runtimeType.toString();

  if (safe && currentRoute == newRoute) return null;

  return navigator.pushReplacement<T, TO>(
    PageTransition(
      type: type ?? PageTransitionType.fade,
      child: page,
      settings: RouteSettings(name: newRoute),
    ),
  );
}