Migration Guide topic
Migration Guide
This document gathered all breaking changes and migrations requirement between versions.
Breaking versions
5.0.0
Summary
getandgetUriinDiohas different signature.DefaultHttpClientAdapteris now namedIOHttpClientAdapter, and the platform independent adapter can be initiated byHttpClientAdapter()which is a factory method.- Adapters that extends
HttpClientAdaptermust nowimplementsinstead ofextends. DioErrorhas separate constructors and all fields are annotated as final.DioErrorTypehas different values.- Imports are split into new libraries:
dio/io.dartis for natives specific classes;dio/browser.dartis for web specific classes.
connectTimeout,sendTimeout, andreceiveTimeoutare nowDurationinstead ofint.
Details
get and getUri
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,
});
HttpClientAdapter
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();
}
Implementing HttpClientAdapter
Before:
class ExampleAdapter extends HttpClientAdapter { /* ... */ }
After:
class ExampleAdapter implements HttpClientAdapter { /* ... */ }
Const DioError
Before:
Never throwDioError() {
final error = DioError(request: requestOptions, error: e);
error.message = 'Custom message.';
error.stackTrace = StackTrace.current;
throw error;
}
After:
Never throwDioError() {
DioError error = DioError(
request: requestOptions,
error: e,
stackTrace: StackTrace.current
);
error = error.copyWith(message: 'Custom message.');
throw error;
}
DioErrorType values update
| Before | After |
|---|---|
| N/A | badCertificate |
| response | badResponse |
| connectTimeout | connectionTimeout |
| other | unknown |
Duration instead of int for timeouts
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),
),
);
}
4.0.0
Details
- Null safety support (Dart >= 2.12).
- The
InterceptorAPIs signature has changed. - Rename
options.mergetooptions.copyWith. - Rename
DioErrorTypeenums from uppercase to camel style. - Delete
dio.resolveanddio.rejectAPIs (usehandlerinstead in interceptors). - Class
BaseOptionsno longer inherits fromOptionsclass. - Change
requestStreamtype ofHttpClientAdapter.fetchfromStream<List<int>>toStream<Uint8List>. - Download API: Add real uri and redirect information to headers.
Libraries
- dio Migration Guide Plugins
- A powerful HTTP client for Dart and Flutter, which supports global settings, Interceptors, FormData, aborting and canceling a request, files uploading and downloading, requests timeout, custom adapters, etc.