change method Null safety
Creates a Request object by changing or augmenting the properties.
When uri
, headers
or body
is specified, they will override the
original corresponding values.
Use patchHeaders
to keep the current headers and also add override its
values (if they already exist) or add values (if they did not exist).
Implementation
Request change({
dynamic uri,
dynamic headers,
dynamic patchHeaders,
dynamic body,
Map<String, dynamic>? form,
Map<String, dynamic>? json,
Map<String, String>? cookies,
c.Encoding? encoding,
}) {
encoding ??= c.utf8;
Uri? parsedUri;
if (uri != null) {
assert(uri is String || uri is Uri);
parsedUri = uri is Uri ? uri : Uri.parse(uri.toString());
}
final newHeaders = headers == null
? this.headers.clone()
: wrapHeaders(headers, clone: true)!;
if (patchHeaders != null) {
final patching = wrapHeaders(patchHeaders)!;
patching.keys.forEach((key) {
newHeaders.remove(key);
newHeaders.add(key, patching[key]);
});
}
final newBody = _buildBody(
body ?? this.body, encoding, newHeaders, form, json, cookies);
return Request._(
method,
parsedUri ?? this.uri,
newHeaders,
newBody,
persistentConnection,
followRedirects,
maxRedirects,
timeout,
);
}