copyWith method

RequestOptions copyWith({
  1. String? method,
  2. Duration? sendTimeout,
  3. Duration? receiveTimeout,
  4. Duration? connectTimeout,
  5. dynamic data,
  6. String? path,
  7. Map<String, dynamic>? queryParameters,
  8. String? baseUrl,
  9. ProgressCallback? onReceiveProgress,
  10. ProgressCallback? onSendProgress,
  11. CancelToken? cancelToken,
  12. Map<String, dynamic>? extra,
  13. Map<String, dynamic>? headers,
  14. bool? preserveHeaderCase,
  15. ResponseType? responseType,
  16. String? contentType,
  17. ValidateStatus? validateStatus,
  18. bool? receiveDataWhenStatusError,
  19. bool? followRedirects,
  20. int? maxRedirects,
  21. bool? persistentConnection,
  22. RequestEncoder? requestEncoder,
  23. ResponseDecoder? responseDecoder,
  24. ListFormat? listFormat,
  25. bool? setRequestContentTypeWhenNoPayload,
})

Create a RequestOptions from current instance with merged attributes.

Implementation

RequestOptions copyWith({
  String? method,
  Duration? sendTimeout,
  Duration? receiveTimeout,
  Duration? connectTimeout,
  dynamic data,
  String? path,
  Map<String, dynamic>? queryParameters,
  String? baseUrl,
  ProgressCallback? onReceiveProgress,
  ProgressCallback? onSendProgress,
  CancelToken? cancelToken,
  Map<String, dynamic>? extra,
  Map<String, dynamic>? headers,
  bool? preserveHeaderCase,
  ResponseType? responseType,
  String? contentType,
  ValidateStatus? validateStatus,
  bool? receiveDataWhenStatusError,
  bool? followRedirects,
  int? maxRedirects,
  bool? persistentConnection,
  RequestEncoder? requestEncoder,
  ResponseDecoder? responseDecoder,
  ListFormat? listFormat,
  bool? setRequestContentTypeWhenNoPayload,
}) {
  final contentTypeInHeader = headers != null &&
      headers.keys
          .map((e) => e.toLowerCase())
          .contains(Headers.contentTypeHeader);

  assert(
    !(contentType != null && contentTypeInHeader),
    'You cannot set both contentType param and a content-type header',
  );

  final ro = RequestOptions(
    method: method ?? this.method,
    sendTimeout: sendTimeout ?? this.sendTimeout,
    receiveTimeout: receiveTimeout ?? this.receiveTimeout,
    connectTimeout: connectTimeout ?? this.connectTimeout,
    data: data ?? this.data,
    path: path ?? this.path,
    baseUrl: baseUrl ?? this.baseUrl,
    queryParameters: queryParameters ?? Map.from(this.queryParameters),
    onReceiveProgress: onReceiveProgress ?? this.onReceiveProgress,
    onSendProgress: onSendProgress ?? this.onSendProgress,
    cancelToken: cancelToken ?? this.cancelToken,
    extra: extra ?? Map.from(this.extra),
    headers: headers ?? Map.from(this.headers),
    preserveHeaderCase: preserveHeaderCase ?? this.preserveHeaderCase,
    responseType: responseType ?? this.responseType,
    validateStatus: validateStatus ?? this.validateStatus,
    receiveDataWhenStatusError:
        receiveDataWhenStatusError ?? this.receiveDataWhenStatusError,
    followRedirects: followRedirects ?? this.followRedirects,
    maxRedirects: maxRedirects ?? this.maxRedirects,
    persistentConnection: persistentConnection ?? this.persistentConnection,
    requestEncoder: requestEncoder ?? this.requestEncoder,
    responseDecoder: responseDecoder ?? this.responseDecoder,
    listFormat: listFormat ?? this.listFormat,
    sourceStackTrace: sourceStackTrace,
  );

  if (contentType != null) {
    ro.headers.remove(Headers.contentTypeHeader);
    ro.contentType = contentType;
  } else if (!contentTypeInHeader) {
    ro.contentType = this.contentType;
  }

  return ro;
}