getParentSpace method

Future<Space?> getParentSpace()

Gets the parent space of this space.

Returns null if this is a root-level space (directly under project). Use parent to get either the parent Space or Project.

final parentSpace = await heroes.getParentSpace();
if (parentSpace != null) {
  print('Parent space: ${parentSpace.path}');
}

Implementation

Future<Space?> getParentSpace() async {
  final parts = path.split('/').where((s) => s.isNotEmpty).toList();
  // If only 1 part (project name), this is root level - no parent space
  if (parts.length <= 1) return null;
  // If 2 parts (project/space), parent is project - no parent space
  if (parts.length == 2) return null;

  final parentPath = '/${parts.sublist(0, parts.length - 1).join('/')}';
  try {
    final response = await client.getSpace(parentPath);
    return Space(response, client);
  } catch (_) {
    return null;
  }
}