back<T> method

void 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, Jet.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

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

Implementation

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

  final delegate = searchDelegate(id);

  // Validate navigator state exists before attempting navigation
  if (delegate.navigatorKey.currentState == null) {
    Jet.log('Cannot navigate back: navigator state is null', isError: true);
    return;
  }

  if (times > 1) {
    var count = 0;
    return delegate.backUntil((route) => count++ == times);
  } else {
    if (canPop) {
      // Check if we can actually pop before attempting
      final canActuallyPop = delegate.canBack;
      if (canActuallyPop == true) {
        return delegate.back<T>(result);
      }
    } else {
      return delegate.back<T>(result);
    }
  }
}