dio 1.0.17

  • Readme
  • Changelog
  • Example
  • Installing
  • 94

Language: English | 中文简体

dio #

build status Pub coverage support

A powerful Http client for Dart, which supports Interceptors, Global configuration, FormData, Request Cancellation, File downloading, Timeout etc.

Add dependency #

  dio: x.x.x  #latest version

Super simple to use #

import 'package:dio/dio.dart';
void getHttp() async {
  try {
    Response response;
    response = await Dio().get("http://www.google.com");
    return print(response);
  } catch (e) {
    return print(e);

Table of contents #

Examples #

Performing a GET request:

  Response response;
  Dio dio = new Dio();
  response = await dio.get("/test?id=12&name=wendu")
// Optionally the request above could also be done as
  response = await dio.get("/test", data: {"id": 12, "name": "wendu"});

Performing a POST request:

response = await dio.post("/test", data: {"id": 12, "name": "wendu"});

Performing multiple concurrent requests:

  response = await Future.wait([dio.post("/info"), dio.get("/token")]);

Downloading a file:

response = await dio.download("https://www.google.com/", "./xx.html");

Sending FormData:

FormData formData = new FormData.from({
    "name": "wendux",
    "age": 25,
  response = await dio.post("/info", data: formData);

Uploading multiple files to server by FormData:

  FormData formData = new FormData.from({
    "name": "wendux",
    "age": 25,
    "file1": new UploadFileInfo(new File("./upload.txt"), "upload1.txt"),
    // upload with bytes (List<int>)
    "file2": new UploadFileInfo.fromBytes(
        utf8.encode("hello world"), "word.txt"),
    // Pass multiple files within an Array
    "files": [
      new UploadFileInfo(new File("./example/upload.txt"), "upload.txt"),
      new UploadFileInfo(new File("./example/upload.txt"), "upload.txt")

Listening the uploading progress:

response = await dio.post(
  data: {"aa": "bb" * 22},
  onUploadProgress: (int sent, int total) {
    print("$sent $total");

…you can find all examples code here.

Dio APIs #

Creating an instance and set default configs. #

You can create instance of Dio with an optional Options object:

Dio dio = new Dio; // with default Options

// Set default configs
  dio.options.baseUrl = "https://www.xx.com/api";
  dio.options.connectTimeout = 5000; //5s
  dio.options.receiveTimeout = 3000;

// or new Dio with a Options instance.
  Options options = new Options(
      baseUrl: "https://www.xx.com/api",
      connectTimeout: 5000,
      receiveTimeout: 3000);
  Dio dio = new Dio(options);

The core API in Dio instance is:


response=await request("/test", data: {"id":12,"name":"xx"}, options: new Options(method:"GET"));

Request method aliases #

For convenience aliases have been provided for all supported request methods.








Future{OnDownloadProgress onProgress, data, bool flush: false, Options options,CancelToken cancelToken})

Request Options #

These are the available config options for making requests. Requests will default to GET if method is not specified.

  /// Http method.
  String method;

  /// Request base url, it can contain sub path, like: "https://www.google.com/api/".
  String baseUrl;

  /// Http request headers.
  Map<String, dynamic> headers;

   /// Timeout in milliseconds for opening  url.
  int connectTimeout;

   ///  Whenever more than [receiveTimeout] (in milliseconds) passes between two events from response stream,
  ///  [Dio] will throw the [DioError] with [DioErrorType.RECEIVE_TIMEOUT].
  ///  Note: This is not the receiving time limitation.
  int receiveTimeout;

  /// Request data, can be any type.
  T data;

  /// If the `path` starts with "http(s)", the `baseURL` will be ignored, otherwise,
  /// it will be combined and then resolved with the baseUrl.
  String path="";

  /// The request Content-Type. The default value is [ContentType.JSON].
  /// If you want to encode request body with "application/x-www-form-urlencoded",
  /// you can set `ContentType.parse("application/x-www-form-urlencoded")`, and [Dio]
  /// will automatically encode the request body.
  ContentType contentType;

  /// [responseType] indicates the type of data that the server will respond with
  /// options which defined in [ResponseType] are `JSON`, `STREAM`, `PLAIN`.
  /// The default value is `JSON`, dio will parse response string to json object automatically
  /// when the content-type of response is "application/json".
  /// If you want to receive response data with binary bytes, for example,
  /// downloading a image, use `STREAM`.
  /// If you want to receive the response data with String, use `PLAIN`.
  ResponseType responseType;
  /// `validateStatus` defines whether the request is successful for a given
  /// HTTP response status code. If `validateStatus` returns `true` ,
  /// the request will be perceived as successful; otherwise, considered as failed.
  ValidateStatus validateStatus;

  /// Custom field that you can retrieve it later in [Interceptor]、[Transformer] and the [Response] object.
  Map<String, dynamic> extra;

There is a complete example here.

Response Schema #

The response for a request contains the following information.

  /// Response body. may have been transformed, please refer to [ResponseType].
  T data;
  /// Response headers.
  HttpHeaders headers;
  /// The corresponding request info.
  Options request;
  /// Http status code.
  int statusCode;
  /// Custom field that you can retrieve it later in `then`.
  Map<String, dynamic> extra;

When request is succeed, you will receive the response as follows:

Response response = await dio.get("https://www.google.com");

Interceptors #

Each Dio instance has a RequestInterceptor and a ResponseInterceptor, by which you can intercept requests or responses before they are handled by then or catchError.

 dio.interceptor.request.onSend = (Options options){
     // Do something before request is sent
     return options; //continue
     // If you want to resolve the request with some custom data,
     // you can return a `Response` object or return `dio.resolve(data)`.
     // If you want to reject the request with a error message, 
     // you can return a `DioError` object or return `dio.reject(errMsg)`    
 dio.interceptor.response.onSuccess = (Response response) {
     // Do something with response data
     return response; // continue
 dio.interceptor.response.onError = (DioError e) {
     // Do something with response error
     return  e;//continue

If you may need to remove an interceptor later you can.

dio.interceptor.request.onSend = null;
dio.interceptor.response.onSuccess = null;
dio.interceptor.response.onError = null;

Resolve and reject the request #

In all interceptors, you can interfere with their execution flow. If you want to resolve the request/response with some custom data,you can return a Response object or return dio.resolve(data). If you want to reject the request/response with a error message, you can return a DioError object or return dio.reject(errMsg) .

 dio.interceptor.request.onSend = (Options options){
     return dio.resolve("fake data")    
 Response response = await dio.get("/test");
 print(response.data);//"fake data"

Supports Async tasks in Interceptors #

Interceptors not only support synchronous tasks, but also supports asynchronous tasks, for example:

  dio.interceptor.request.onSend = (Options options) async{
     //...If no token, request token firstly.
     Response response = await dio.get("/token");
     //Set the token to headers 
     options.headers["token"] = response.data["data"]["token"];
     return options; //continue   

Lock/unlock the interceptors #

You can lock/unlock the interceptors by calling their lock()/unlock method. Once the request/response interceptor is locked, the incoming request/response will be added to a queue before they enter the interceptor, they will not be continued until the interceptor is unlocked.

  tokenDio = new Dio(); //Create a new instance to request the token.
  tokenDio.options = dio;
  dio.interceptor.request.onSend = (Options options) async {
    // If no token, request token firstly and lock this interceptor
    // to prevent other request enter this interceptor.
    // We use a new Dio(to avoid dead lock) instance to request token.
    Response response = await tokenDio.get("/token");
    //Set the token to headers
    options.headers["token"] = response.data["data"]["token"];
    return options; //continue

You can clean the waiting queue by calling clear();

aliases #

When the request interceptor is locked, the incoming request will pause, this is equivalent to we locked the current dio instance, Therefore, Dio provied the two aliases for the lock/unlock of request interceptors.

dio.lock() == dio.interceptor.request.lock()

dio.unlock() == dio.interceptor.request.unlock()

Example #

Because of security reasons, we need all the requests to set up a csrfToken in the header, if csrfToken does not exist, we need to request a csrfToken first, and then perform the network request, because the request csrfToken progress is asynchronous, so we need to execute this async request in request interceptor. The code is as follows:

dio.interceptor.request.onSend = (Options options) {
    print('send request:path:${options.path},baseURL:${options.baseUrl}');
    if (csrfToken == null) {
      print("no token,request token firstly...");
      //lock the dio.
      return tokenDio.get("/token").then((d) {
        options.headers["csrfToken"] = csrfToken = d.data['data']['token'];
        print("request token succeed, value: " + d.data['data']['token']);
        print('continue to perform request:path:${options.path},baseURL:${options.path}');
        return options;
      }).whenComplete(() => dio.unlock()); // unlock the dio
    } else {
      options.headers["csrfToken"] = csrfToken;
      return options;

For complete codes click here.

Handling Errors #

When a error occurs, Dio will wrap the Error/Exception to a DioError:

  try {
    await dio.get("https://wendux.github.io/xsddddd");
   } on DioError catch(e) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx and is also not 304.
      if(e.response) {
      } else{
        // Something happened in setting up or sending the request that triggered an Error  

DioError scheme #

  /// Response info, it may be `null` if the request can't reach to
  /// the http server, for example, occurring a dns error, network is not available.
  Response response;

  /// Error descriptions.
  String message;
  DioErrorType type;

  /// Error stacktrace info
  StackTrace stackTrace;

DioErrorType #

enum DioErrorType {
  /// Default error type, usually occurs before connecting the server.

  /// When opening  url timeout, it occurs.

  ///  Whenever more than [receiveTimeout] (in milliseconds) passes between two events from response stream,
  ///  [Dio] will throw the [DioError] with [DioErrorType.RECEIVE_TIMEOUT].
  ///  Note: This is not the receiving time limitation.

  /// When the server response, but with a incorrect status, such as 404, 503...

  /// When the request is cancelled, dio will throw a error with this type.

Using application/x-www-form-urlencoded format #

By default, Dio serializes request data(except String type) to JSON. To send data in the application/x-www-form-urlencoded format instead, you can :

//Instance level
//or works once
dio.post("/info",data:{"id":5}, options: new Options(contentType:ContentType.parse("application/x-www-form-urlencoded")));

There is an example here.

Sending FormData #

You can also send FormData with Dio, which will send data in the multipart/form-data, and it supports uploading files.

FormData formData = new FormData.from({
    "name": "wendux",
    "age": 25,
    "file": new UploadFileInfo(new File("./example/upload.txt"), "upload.txt")
response = await dio.post("/info", data: formData);

Note: Just the post method suppots FormData.

There is a complete example here.

Transformer #

Transformer allows changes to the request/response data before it is sent/received to/from the server. This is only applicable for request methods 'PUT', 'POST', and 'PATCH'. Dio has already implemented a DefaultTransformer, and as the default Transformer. If you want to customize the transformation of request/response data, you can provide a Transformer by your self, and replace the DefaultTransformer by setting the dio.transformer.

There is an example for customizing Transformer.

Set proxy and HttpClient config #

Dio uses HttpClient to send http request, so you can config the dio.httpClient to support proxy, for example:

  dio.onHttpClientCreate = (HttpClient client) {
    // config the http client  
    client.findProxy = (uri) {
      //proxy all request to localhost:8888
      return "PROXY localhost:8888";
    // you can also create a new HttpClient to dio
    // return new HttpClient();  

There is a complete example here.

Https certificate verification #

There are two ways to verify the https certificate. Suppose the certificate format is PEM, the code like:

  String PEM="XXXXX"; // certificate content 
  dio.onHttpClientCreate = (HttpClient client) {
    client.badCertificateCallback=(X509Certificate cert, String host, int port){
      if(cert.pem==PEM){ // Verify the certificate
        return true; 
      return false;

Another way is creating a SecurityContext when create the HttpClient:

  dio.onHttpClientCreate = (HttpClient client) {
    SecurityContext sc = new SecurityContext();
    //file is the path of certificate
    HttpClient httpClient = new HttpClient(context: sc);
    return httpClient;

In this way, the format of certificate must be PEM or PKCS12.

Cancellation #

You can cancel a request using a cancel token. One token can be shared with multiple requests. When a token's cancel method invoked, all requests with this token will be cancelled.

CancelToken token = new CancelToken();
dio.get(url1, cancelToken: token);
dio.get(url2, cancelToken: token);

// cancel the requests with "cancelled" message.

There is a complete example here.

You can manage the request/response cookies using cookieJar .

The dio cookie manage API is based on the withdrawn cookie_jar.

You can create a CookieJar or PersistCookieJar to manage cookies automatically, and dio use the CookieJar by default, which saves the cookies in RAM. If you want to persists cookies, you can use the PersistCookieJar class, the example codes as follows:

var dio = new Dio();
dio.cookieJar=new PersistCookieJar("./cookies");

PersistCookieJar is a cookie manager which implements the standard cookie policy declared in RFC. PersistCookieJar persists the cookies in files, so if the application exit, the cookies always exist unless call delete explicitly.

More details about cookie_jar see : https://github.com/flutterchina/cookie_jar .

This open source project authorized by https://flutterchina.club , and the license is MIT.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

0.1.8 #

  • change file name "TransFormer" to "Transformer"
  • change "dio.transFormer" to "dio.transformer"
  • change deprecated "UTF8" to "utf8"

0.1.5 #

  • add clear method for dio instance

0.1.4 #

  • fix download bugs

0.1.3 #

  • support upload files with Array
  • support create HttpClient by user self in onHttpClientCreate
  • support generic
  • bug fix

0.0.1 #

  • Initial version, created by Stagehand


import 'dart:io';
import 'package:dio/dio.dart';

main() async {
  var dio = new Dio();
  dio.options.baseUrl = "http://www.dtworkroom.com/doris/1/2.0.0/";
  dio.options.connectTimeout = 5000; //5s
  dio.options.receiveTimeout = 5000;
  dio.options.headers = {
    'user-agent': 'dio',
    'common-header': 'xx'

  var u = new Uri(scheme: "https", host: "www.baidu.com", queryParameters: {
    "xx": "xx",
    "yy": "dd"


  // Add request interceptor
  dio.interceptor.request.onSend = (Options options) async {
    // return ds.resolve(new Response(data:"xxx"));
    // return ds.reject(new DioError(message: "eh"));
    return options;

  Response response = await dio.get("https://www.google.com/");

  // Download a file
  response = await dio.download(
      "https://www.google.com/", "./xx.html", onProgress: (received, total) {

  // Create a FormData
  FormData formData = new FormData.from({
    "name": "wendux",
    "age": 25,
    "file": new UploadFileInfo(new File("./example/upload.txt"), "upload.txt")

  // Send FormData
  response = await dio.post("/test", data: formData);

  response = await dio.post("/test",
    data: {
      "id": 8,
      "info": {
        "name": "wendux",
        "age": 25
    // Send data with "application/x-www-form-urlencoded" format
    options: new Options(
        contentType: ContentType.parse("application/x-www-form-urlencoded")),

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  dio: ^1.0.17

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:dio/dio.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Oct 8, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21


Detected platforms: Flutter, other

Primary library: package:dio/dio.dart with components: io.

Health suggestions

Fix lib/src/dio.dart. (-4.41 points)

Analysis of lib/src/dio.dart reported 9 hints, including:

line 232 col 3: Prefer using /// for doc comments.

line 243 col 3: Prefer using /// for doc comments.

line 252 col 3: Prefer using /// for doc comments.

line 259 col 3: Prefer using /// for doc comments.

line 292 col 17: Use = to separate a named parameter from its default value.

Fix lib/src/interceptor.dart. (-3.45 points)

Analysis of lib/src/interceptor.dart reported 7 hints, including:

line 12 col 1: Prefer using /// for doc comments.

line 22 col 3: Prefer using /// for doc comments.

line 36 col 3: Prefer using /// for doc comments.

line 46 col 3: Prefer using /// for doc comments.

line 56 col 3: Prefer using /// for doc comments.

Fix lib/src/form_data.dart. (-1.99 points)

Analysis of lib/src/form_data.dart reported 4 hints:

line 7 col 1: Prefer using /// for doc comments.

line 24 col 3: Prefer using /// for doc comments.

line 97 col 9: Use isNotEmpty instead of length

line 97 col 28: Use isNotEmpty instead of length

Fix additional 6 files with analysis or formatting issues. (-3 points)

Additional issues in the following files:

  • lib/src/options.dart (2 hints)
  • lib/src/cancel_token.dart (1 hint)
  • lib/src/dio_error.dart (1 hint)
  • lib/src/response.dart (1 hint)
  • lib/src/upload_file_info.dart (1 hint)
  • lib/src/transformer.dart (Run dartfmt to format lib/src/transformer.dart.)

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (cookie_jar).


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.9.0 <3.0.0
cookie_jar ^0.0.7 0.0.8 1.0.1
Dev dependencies
test ^1.5.1
test_coverage ^0.2.0