destination property

Destination get destination

Encodes this complete branch into one canonical destination.

Implementation

Destination get destination {
  final segments = <String>[];
  final query = <String, List<String>>{};
  for (final ref in _refs) {
    segments.addAll(ref.buildPath());
    for (final entry in ref.buildQuery().entries) {
      if (query.containsKey(entry.key)) {
        throw StateError(
          'Search parameter "${entry.key}" is encoded by more than one '
          'route reference.',
        );
      }
      query[entry.key] = entry.value;
    }
  }
  final target = _refs.last.routeNode;
  if (!target.terminal) {
    throw StateError('A destination must end at a terminal route.');
  }
  return Destination._(
    route: target,
    uri: segments.isEmpty
        ? Uri(path: '/', queryParameters: query.isEmpty ? null : query)
        : Uri(
            pathSegments: <String>['', ...segments],
            queryParameters: query.isEmpty ? null : query,
          ),
  );
}