parseFragment function

String parseFragment(
  1. String location
)

Extracts the hash fragment from a URL or location string without the leading #.

If no hash fragment is present in location, returns an empty string.

Safe for both server-side rendering (SSR) and browser environments.

final fragment = parseFragment('/docs/guide#getting-started');
print(fragment); // 'getting-started'

Implementation

String parseFragment(String location) {
  final hashIdx = location.indexOf('#');
  if (hashIdx == -1) return '';
  return location.substring(hashIdx + 1);
}