replace method

  1. @override
Uri replace({
  1. String? scheme,
  2. String? userInfo,
  3. String? host,
  4. int? port,
  5. String? path,
  6. Iterable<String>? pathSegments,
  7. String? query,
  8. Map<String, dynamic>? queryParameters,
  9. String? fragment,
})
override

Creates a new Uri based on this one, but with some parts replaced.

This method takes the same parameters as the Uri constructor, and they have the same meaning.

At most one of path and pathSegments must be provided. Likewise, at most one of query and queryParameters must be provided.

Each part that is not provided will default to the corresponding value from this Uri instead.

This method is different from Uri.resolve, which overrides in a hierarchical manner, and can instead replace each part of a Uri individually.

Example:

final uri1 = Uri.parse(
    'http://dart.dev/guides/libraries/library-tour#utility-classes');

final uri2 = uri1.replace(
    scheme: 'https',
    path: 'guides/libraries/library-tour',
    fragment: 'uris');
print(uri2); // https://dart.dev/guides/libraries/library-tour#uris

This method acts similarly to using the Uri constructor with some of the arguments taken from this Uri. Example:

final Uri uri3 = Uri(
    scheme: 'https',
    userInfo: uri1.userInfo,
    host: uri1.host,
    port: uri2.port,
    path: '/guides/language/language-tour',
    query: uri1.query,
    fragment: null);
print(uri3); // https://dart.dev/guides/language/language-tour

Using this method can be seen as shorthand for the Uri constructor call above, but may also be slightly faster because the parts taken from this Uri need not be checked for validity again.

Implementation

@override
Uri replace(
    {String? scheme,
    String? userInfo,
    String? host,
    int? port,
    String? path,
    Iterable<String>? pathSegments,
    String? query,
    Map<String, dynamic>? queryParameters,
    String? fragment}) {
  return uri.replace(
      scheme: scheme,
      userInfo: userInfo,
      host: host,
      port: port,
      path: path,
      pathSegments: pathSegments,
      query: query,
      queryParameters: queryParameters,
      fragment: fragment);
}