back<T> method

bool back<T>({
  1. T? result,
  2. bool canPop = true,
  3. int times = 1,
  4. String? id,
})

Navigation.popUntil() shortcut.

Pop the current page, snackbar, dialog or bottomsheet in the stack

if your set closeOverlays to true, Get.back() will close the currently open snackbar/dialog/bottomsheet AND the current page

id is for when you are using nested navigation, as explained in documentation

When the topmost route handles the pop internally — for example a Scaffold with an open drawer, or a persistent bottom sheet, both of which register a LocalHistoryEntry on the enclosing route — that entry is consumed (e.g. the drawer closes) and the page itself stays, matching Navigator.pop semantics.

It has the advantage of not needing context, so you can call from your business logic.

Returns whether the back navigation was performed: false when canPop is true but there is no route to go back to (e.g. the current page is the only one in the stack, as after a deep link), so callers can detect the ignored back and react, true otherwise.

Implementation

bool back<T>({T? result, bool canPop = true, int times = 1, String? id}) {
  if (times < 1) {
    times = 1;
  }

  if (times > 1) {
    var count = 0;
    searchDelegate(id).backUntil((route) => count++ == times);
    return true;
  } else {
    if (_topRoute(id: id)?.willHandlePopInternally == true) {
      searchDelegate(id).navigatorKey.currentState?.pop<T>(result);
      return true;
    }
    if (canPop) {
      if (searchDelegate(id).canBack == true) {
        searchDelegate(id).back<T>(result);
        return true;
      }
      return false;
    } else {
      searchDelegate(id).back<T>(result);
      return true;
    }
  }
}