setFragment method

Future<void> setFragment(
  1. String fragment, {
  2. bool replace = false,
})

Updates the hash fragment on the current path, either pushing or replacing history.

fragment may optionally include or omit the leading #.

await controller.setFragment('top', replace: true);

Implementation

Future<void> setFragment(String fragment, {bool replace = false}) async {
  final cleanFrag = fragment.startsWith('#') ? fragment.substring(1) : fragment;
  final qs = buildQueryString(currentQueryAll.value);
  final hashPart = cleanFrag.isNotEmpty ? '#$cleanFrag' : '';
  final target = '${currentPath.value}$qs$hashPart';
  if (replace) {
    await this.replace(target);
  } else {
    await navigate(target);
  }
}