extract method
Extracts an ID from a route string.
Parameters:
baseRoute: The base route string, which is to be excluded from the extracted ID.route: The complete route string from which the ID is to be extracted.
Returns:
- The extracted ID of type
TId. IfTIdisString, the ID is returned as a string. IfTIdisint, the last segment is parsed as an integer. - Returns
nullif the ID segment is empty, or if the route does not contain a valid ID.
Throws:
SimApiExceptionifTIdis not supported (i.e., neitherStringnorint).
Implementation
TId? extract(String baseRoute, String route) {
final segments = route.split('/');
if (segments.isEmpty) return null;
// Remove the base route from the segments if it is present.
final baseSegments = baseRoute.split('/');
final idSegments = segments.sublist(baseSegments.length);
// The ID is the last segment.
final idSegment = idSegments.isNotEmpty ? idSegments.last : null;
if (idSegment == null || idSegment.isEmpty) return null;
if (TId == String) {
return idSegment as TId;
} else if (TId == int) {
final id = int.tryParse(idSegment);
if (id == null) return null;
return id as TId;
} else {
throw SimApiException('Unsupported ID type: ${TId.toString()}');
}
}