getPathFromName method
GetPathFromNameResult
getPathFromName(
- String nameToMatch, {
- required Map<
String, String> pathParameters, - required GetNewParentPathResult parentPathResult,
- required Map<
String, String> remainingPathParameters,
inherited
This function takes a name and tries to find the path corresponding to the route matching this name
The deeper nested the route the better The given path parameters have to include at least every path parameters of the final path
Implementation
GetPathFromNameResult getPathFromName(
String nameToMatch, {
required Map<String, String> pathParameters,
required GetNewParentPathResult parentPathResult,
required Map<String, String> remainingPathParameters,
}) {
final childNameResults = <GetPathFromNameResult>[];
// Check if any subroute matches the name
for (var vRouteElement in _subroutes) {
childNameResults.add(
vRouteElement.getPathFromName(
nameToMatch,
pathParameters: pathParameters,
parentPathResult: parentPathResult,
remainingPathParameters: remainingPathParameters,
),
);
if (childNameResults.last is ValidNameResult) {
return childNameResults.last;
}
}
// If we don't have any valid result
// If some stackedRoute returned PathParamsPopError, aggregate them
final pathParamsNameErrors = PathParamsErrorsNameResult(
name: nameToMatch,
values: childNameResults.fold<List<PathParamsError>>(
<PathParamsError>[],
(previousValue, element) {
return previousValue +
((element is PathParamsErrorsNameResult) ? element.values : []);
},
),
);
// If there was any PathParamsPopError, we have some pathParamsPopErrors.values
// and therefore should return this
if (pathParamsNameErrors.values.isNotEmpty) {
return pathParamsNameErrors;
}
// Else try to find a NullPathError
if (childNameResults.indexWhere(
(childNameResult) => childNameResult is NullPathErrorNameResult) !=
-1) {
return NullPathErrorNameResult(name: nameToMatch);
}
// Else return a NotFoundError
return NotFoundErrorNameResult(name: nameToMatch);
}