This document gathered all breaking changes and migrations requirement between versions.
- All request will not imply the default content type by default, leave to users to handle.
get
andgetUri
inDio
has different signature.DefaultHttpClientAdapter
is now namedIOHttpClientAdapter
, and the platform independent adapter can be initiated byHttpClientAdapter()
which is a factory method.- Adapters that extends
HttpClientAdapter
must nowimplements
instead ofextends
. DioError
has separate constructors and all fields are annotated as final.DioErrorType
has different values.- Imports are split into new libraries:
dio/io.dart
is for natives specific classes;dio/browser.dart
is for web specific classes.
connectTimeout
,sendTimeout
, andreceiveTimeout
are nowDuration
instead ofint
.
Now you'll need to specify the content type in the BaseOptions
or in every requests' Options
or headers. To do so:
- In
BaseOptions
:dio.options.contentType = Headers.jsonContentType;
- In
Options
:dio.get('some/path', options: Options(contentType: Headers.jsonContentType));
- In headers:
dio.get( 'some/path', options: Options( headers: {Headers.contentTypeHeader: Headers.jsonContentType}, ), );
If you have your own request method that wraps diox
:
void request(
Uri uri, {
Map<String, String?>? queryParameters,
Object? body,
Map<String, dynamic>? headers,
Options? options,
ResponseType? responseType = ResponseType.json,
}) {
if (body != null) {
// If you have `content-type` in `headers`, it will be use first.
options.contentType ??= Headers.jsonContentType;
}
}
Future<Response<T>> get<T>(
String path, {
+ Object? data,
Map<String, dynamic>? queryParameters,
Options? options,
CancelToken? cancelToken,
ProgressCallback? onReceiveProgress,
});
Future<Response<T>> getUri<T>(
Uri uri, {
+ Object? data,
Map<String, dynamic>? queryParameters,
Options? options,
CancelToken? cancelToken,
ProgressCallback? onReceiveProgress,
});
Before:
void initAdapter() {
final dio = Dio();
// For natives.
dio.httpClientAdapter = DefaultHttpClientAdapter();
// For web.
dio.httpClientAdapter = BrowserHttpClientAdapter();
}
After:
void initAdapter() {
final dio = Dio();
// Universal adapter that create the adapter for the corresponding platform.
dio.httpClientAdapter = HttpClientAdapter();
// For natives.
dio.httpClientAdapter = IOHttpClientAdapter();
// For web.
dio.httpClientAdapter = BrowserHttpClientAdapter();
}
Before:
class ExampleAdapter extends HttpClientAdapter { /* ... */ }
After:
class ExampleAdapter implements HttpClientAdapter { /* ... */ }
Before:
Never throwDioError() {
final error = DioError(request: requestOptions, error: err);
error.message = 'Custom message.';
error.stackTrace = StackTrace.current;
throw error;
}
After:
Never throwDioError() {
DioError error = DioError(
request: requestOptions,
error: err,
stackTrace: StackTrace.current
);
error = error.copyWith(message: 'Custom message.');
throw error;
}
Before | After |
---|---|
N/A | badCertificate |
response | badResponse |
connectTimeout | connectionTimeout |
other | unknown |
Before:
void request() {
final dio = Dio(
BaseOptions(
connectTimeout: 5000,
sendTimeout: 5000,
receiveTimeout: 10000,
),
);
}
After:
void request() {
final dio = Dio(
BaseOptions(
connectTimeout: const Duration(seconds: 5),
sendTimeout: const Duration(seconds: 5),
receiveTimeout: const Duration(seconds: 10),
),
);
}
- Null safety support (Dart >= 2.12).
- The
Interceptor
APIs signature has changed. - Rename
options.merge
tooptions.copyWith
. - Rename
DioErrorType
enums from uppercase to camel style. - Delete
dio.resolve
anddio.reject
APIs (usehandler
instead in interceptors). - Class
BaseOptions
no longer inherits fromOptions
class. - Change
requestStream
type ofHttpClientAdapter.fetch
fromStream<List<int>>
toStream<Uint8List>
. - Download API: Add real uri and redirect information to headers.