rebuild method
Uri
rebuild({
- String? schemeBuilder(
- String current
- String? userInfoBuilder(
- String current
- String? hostBuilder(
- String current
- int? portBuilder(
- int current
- String? pathBuilder(
- String current
- Iterable<
String> ? pathSegmentsBuilder()?, - String? queryBuilder(
- String current
- Map<
String, dynamic> ? queryParametersBuilder()?, - String? fragmentBuilder(
- String current
Creates a new Uri based on this one by applying builder functions
to individual components.
Each builder, if provided, receives the current value and should return the new value. If a builder isn't provided, the current value is retained.
If both pathBuilder and pathSegmentsBuilder return non-null values,
pathSegmentsBuilder takes precedence.
If both queryBuilder and queryParametersBuilder return non-null values,
queryParametersBuilder takes precedence.
Implementation
Uri rebuild({
String? Function(String current)? schemeBuilder,
String? Function(String current)? userInfoBuilder,
String? Function(String current)? hostBuilder,
int? Function(int current)? portBuilder,
String? Function(String current)? pathBuilder,
Iterable<String>? Function(Iterable<String> current)? pathSegmentsBuilder,
String? Function(String current)? queryBuilder,
Map<String, dynamic>? Function(Map<String, dynamic> current)?
queryParametersBuilder,
String? Function(String current)? fragmentBuilder,
}) {
// Initialize replacement variables with null to indicate "no change"
String? newScheme;
String? newUserInfo;
String? newHost;
int? newPort;
String? newPath;
List<String>? newPathSegments;
String? newQuery;
Map<String, dynamic>? newQueryParameters;
String? newFragment;
// Apply builder functions if provided
if (schemeBuilder != null) {
newScheme = schemeBuilder(scheme);
}
if (userInfoBuilder != null) {
newUserInfo = userInfoBuilder(userInfo);
}
if (hostBuilder != null) {
newHost = hostBuilder(host);
}
if (portBuilder != null) {
newPort = portBuilder(port);
}
if (pathBuilder != null) {
newPath = pathBuilder(path);
}
if (pathSegmentsBuilder != null) {
final segments = pathSegmentsBuilder(pathSegments);
if (segments != null) {
newPathSegments = segments.toList();
}
}
if (queryBuilder != null) {
newQuery = queryBuilder(query);
}
if (queryParametersBuilder != null) {
// Convert Map<String, String> to Map<String, dynamic>
final dynamicQueryParams = Map<String, dynamic>.from(queryParameters);
// Apply the builder
final resultParams = queryParametersBuilder(dynamicQueryParams);
// Convert back to Map<String, dynamic> allowing Iterables
if (resultParams != null) {
newQueryParameters = {};
resultParams.forEach((key, value) {
if (value is Iterable) {
newQueryParameters![key] = value.map((e) => e.toString()).toList();
} else {
newQueryParameters![key] = value?.toString() ?? '';
}
});
}
}
if (fragmentBuilder != null) {
newFragment = fragmentBuilder(fragment);
}
final effectivePathSegments = newPathSegments;
final effectivePath = effectivePathSegments == null ? newPath : null;
final effectiveQueryParameters = newQueryParameters;
final effectiveQuery = effectiveQueryParameters == null ? newQuery : null;
// Create a new Uri with the modified components
return replace(
scheme: newScheme,
userInfo: newUserInfo,
host: newHost,
port: newPort,
path: effectivePath,
pathSegments: effectivePathSegments,
query: effectiveQuery,
queryParameters: effectiveQueryParameters,
fragment: newFragment,
);
}