close

flutter remove all routes method

  1. @optionalTypeArgs

Future<T> pushAndRemoveUntil <T extends Object>(

  1. Route<T> newRoute,
  2. RoutePredicate predicate

)@optionalTypeArgs

Push the given course onto the pilot, and afterward eliminate all the past courses until the predicate brings valid back.

The predicate might be applied to a similar course more than once if Route.willHandlePopInternally is valid.

To eliminate courses until a course with a specific name, utilize the RoutePredicate got back from ModalRoute.withName.

To eliminate all the courses beneath the pushed course, utilize a RoutePredicate that consistently returns bogus (for example (Route course) => bogus).

The eliminated courses are taken out without being finished, so this strategy doesn’t take a return esteem contention.

The recently pushed course and its former course are informed for Route.didPush. After expulsion, the new course and its new going before course, (the course beneath the bottommost eliminated course) are advised through Route.didChangeNext). On the off chance that the Navigator has any Navigator.observers, they will be informed also (see NavigatorObserver.didPush and NavigatorObserver.didRemove). The eliminated courses are discarded and advised, when the new course has got done with vitalizing. The fates that had been gotten back from pushing those courses won’t finish.

Continuous signals inside the current course are dropped when another course is pushed.

Returns a Future that finishes to the outcome esteem passed to pop when the pushed course is flown off the guide.

The T type contention is the sort of the return estimation of the new course.

void _resetAndOpenPage() {
  navigator.pushAndRemoveUntil(
    MaterialPageRoute(builder: (BuildContext context) => MyHomePage()),
    ModalRoute.withName('/'),
  );
}

Implementation

@optionalTypeArgs
Future<T> pushAndRemoveUntil<T extends Object>(Route<T> newRoute, RoutePredicate predicate) {
  assert(!_debugLocked);
  assert(() {
    _debugLocked = true;
    return true;
  }());
  assert(newRoute != null);
  assert(newRoute._navigator == null);
  assert(newRoute.overlayEntries.isEmpty);
  assert(predicate != null);
  int index = _history.length - 1;
  _history.add(_RouteEntry(newRoute, initialState: _RouteLifecycle.push));
  while (index >= 0 && !predicate(_history[index].route)) {
    if (_history[index].isPresent)
      _history[index].remove();
    index -= 1;
  }
  _flushHistoryUpdates();

  assert(() {
    _debugLocked = false;
    return true;
  }());
  _afterNavigation(newRoute);
  return newRoute.popped;
}

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment