getPathFromPop method

VPopResult getPathFromPop(
  1. VRouteElement elementToPop, {
  2. required Map<String, String> pathParameters,
  3. required GetNewParentPathResult parentPathResult,
})
inherited

VPopResult.didPop is true if this VRouteElement popped VPopResult.extendedPath is null if this path can't be the right one according to the path parameters VPopResult is null when this VRouteElement does not pop AND none of its stackedRoutes popped

Implementation

VPopResult getPathFromPop(
  VRouteElement elementToPop, {
  required Map<String, String> pathParameters,
  required GetNewParentPathResult parentPathResult,
}) {
  // If vRouteElement is this, then this is the element to pop so we return null
  if (elementToPop == this) {
    return PoppingPopResult(poppedVRouteElements: [this]);
  }

  // Try to pop from the stackedRoutes
  for (var vRouteElement in _subroutes) {
    VPopResult childPopResult = vRouteElement.getPathFromPop(
      elementToPop,
      pathParameters: pathParameters,
      parentPathResult: parentPathResult,
    );
    if (!(childPopResult is NotFoundPopResult)) {
      // If the VRouteElement to pop has been found

      // If NOT PoppingPopResult, return the PathParamsPopErrors or the ValidPopResult as is
      if (!(childPopResult is PoppingPopResult)) {
        // If it is ValidPopResult, add this name to the list of names if this has a name
        if (childPopResult is ValidPopResult) {
          return ValidPopResult(
              path: childPopResult.path,
              poppedVRouteElements: childPopResult.poppedVRouteElements,
              names: ((this is VRouteElementWithName) &&
                          (this as VRouteElementWithName).name != null
                      ? [(this as VRouteElementWithName).name!]
                      : <String>[]) +
                  childPopResult.names);
        }

        return childPopResult;
      }

      // If PoppingPopResult, check whether we should pop with it or not

      if (mustHaveSubRoutes) {
        // If we should pop with the VRouteElement to pop
        // Add ourselves to the poppedVRouteElements in a PoppingPopResult
        return PoppingPopResult(
          poppedVRouteElements: childPopResult.poppedVRouteElements + [this],
        );
      }

      // If we should NOT pop with the VRouteElement to pop

      final poppedVRouteElements = childPopResult.poppedVRouteElements;

      // Check whether the parentPathResult is valid or not
      if (parentPathResult is ValidParentPathResult) {
        // If parentPathResult is valid, return a ValidPopResult with the right path
        return ValidPopResult(
          path: parentPathResult.path,
          poppedVRouteElements: poppedVRouteElements,
          names: (this is VRouteElementWithName) &&
                  (this as VRouteElementWithName).name != null
              ? [(this as VRouteElementWithName).name!]
              : [],
        );
      }

      // Else return a PathParamsPopErrors by specifying what prevented parentPathResult from
      // being valid
      assert(parentPathResult is PathParamsErrorNewParentPath);
      return PathParamsPopErrors(
        poppedVRouteElements: poppedVRouteElements,
        values: [
          MissingPathParamsError(
            pathParams: pathParameters.keys.toList(),
            missingPathParams:
                (parentPathResult as PathParamsErrorNewParentPath)
                    .pathParameters,
          ),
        ],
      );
    }
  }

  // If none of the stackedRoutes popped and this did not pop, return a NotValidPopResult
  // This should never reach RootVRouter
  return NotFoundPopResult();
}