replace method Null safety

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}

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

This method takes the same parameters as the new 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.


Uri uri1 = Uri.parse("a://b@c:4/d/e?f#g");
Uri uri2 = uri1.replace(scheme: "A", path: "D/E/E", fragment: "G");
print(uri2);  // prints "a://b@c:4/D/E/E?f#G"

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

Uri uri3 = new Uri(
    scheme: "A",
    userInfo: uri1.userInfo,
    port: uri1.port,
    path: "D/E/E",
    query: uri1.query,
    fragment: "G");
print(uri3);  // prints "a://b@c:4/D/E/E?f#G"
print(uri2 == uri3);  // prints true.

Using this method can be seen as a 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.


Uri replace(
    {String? scheme,
    String? userInfo,
    String? host,
    int? port,
    String? path,
    Iterable<String>? pathSegments,
    String? query,
    Map<String, dynamic /*String|Iterable<String>*/ >? queryParameters,
    String? fragment});