to method

void to(
  1. String path, {
  2. Map<String, String> queryParameters = const {},
  3. String? hash,
  4. Map<String, String> historyState = const {},
  5. dynamic isReplacement = false,
})

The main method to navigate to a new path

Note that the path should be a valid url. If you fear part of you url might need encoding, use toSegments instead

path can be of one of two forms:

  • stating with '/', in which case we just navigate to the given path
  • not starting with '/', in which case we append the current path to the given one

hash will be added after a hash sign (#) in the url (this will not appear if empty)

queryParameters to add query parameters (you can also add them manually)

historyState is used an the web to restore browser history entry specific state (like scroll amount)

isReplacement determines whether to overwrite the current history entry or create a new one. The is mainly useful when using back, forward or historyGo, or on the web to control the browser history entries

Also see:

  • toSegments if you need your path segments to be encoded
  • toNamed if you want to navigate by name
  • toExternal if you want to navigate to an external url

Implementation

void to(
  String path, {
  Map<String, String> queryParameters = const {},
  String? hash,
  Map<String, String> historyState = const {},
  isReplacement = false,
}) {
  // Don't display the hash if it is empty
  final _hash = (hash?.isEmpty ?? true) ? null : hash;

  if (!path.startsWith('/')) {
    if (url == null) {
      throw InvalidUrlVError(url: path);
    }
    final currentPath = Uri.parse(url!).path;
    path = currentPath + (currentPath.endsWith('/') ? '' : '/') + path;
  }

  // Extract query parameters if any was passed directly in [path]
  final pathUri = Uri.parse(path);

  final pathQueryParameters = pathUri.queryParameters;
  final pathHash = pathUri.fragment.isEmpty ? null : pathUri.fragment;
  path = pathUri.path; // Update the path if there where queryParameters

  assert(
    pathQueryParameters.isEmpty || queryParameters.isEmpty,
    'Some path parameters where passed using [path] AND other using [queryParameters]\n'
    'Use one or the other but not both',
  );

  final uri = Uri(
    path: path,
    queryParameters: queryParameters.isNotEmpty
        ? queryParameters
        : pathQueryParameters.isNotEmpty
            ? pathQueryParameters
            : null,
    fragment: _hash ?? pathHash,
  );

  _updateUrl(
    uri,
    newVRoute: _getNewVRoute(uri: uri, historyState: historyState),
    newHistoryState: historyState,
    onCancel: () {
      VLogPrinter.show(
        VStoppedNavigationTo(
          vNavigationMethod: VNavigationMethod.to,
          url: uri.toString(),
        ),
      );
    },
    onUpdate: () {
      final _updateLocation = isReplacement
          ? _vRouterScope.vHistory.replaceLocation
          : _vRouterScope.vHistory.pushLocation;

      _updateLocation(
        VRouteInformation(
          url: uri.toString(),
          state: historyState,
        ),
      );
      VLogPrinter.show(
        VSuccessfulNavigationTo(
          vNavigationMethod: VNavigationMethod.to,
          url: uri.toString(),
        ),
      );
    },
  );
}