toNamed method

void toNamed(
  1. String name, {
  2. Map<String, String> pathParameters = const {},
  3. Map<String, String> queryParameters = const {},
  4. String? hash,
  5. Map<String, String> historyState = const {},
  6. bool isReplacement = false,
})

pathParameters needs to specify every path parameters contained in the path corresponding to name

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:

  • to if you don't need segment encoding
  • toSegments if you need your path segments to be encoded
  • toExternal if you want to navigate to an external url

Implementation

void toNamed(
  String name, {
  Map<String, String> pathParameters = const {},
  Map<String, String> queryParameters = const {},
  String? hash,
  Map<String, String> historyState = const {},
  bool isReplacement = false,
}) {
  final uri = Uri(
    path: _getUrlFromName(
      name,
      pathParameters: pathParameters,
    ),
    queryParameters: queryParameters.isNotEmpty ? queryParameters : null,
    fragment: (hash?.isNotEmpty ?? false) ? hash : null,
  );

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

      // If the navigation is successful, sync the vHistory
      // This also pushes to the browser if needed
      _updateLocation(
        VRouteInformation(
          url: uri.toString(),
          state: historyState,
        ),
      );
      VLogPrinter.show(
        VSuccessfulNavigationTo(
          vNavigationMethod: VNavigationMethod.toNamed,
          url: uri.toString(),
        ),
      );
    },
  );
}