<>dio

dio It's a powerful Dart Http Request Library , support Restful API,FormData, Interceptor , Request cancellation ,Cookie Administration , File upload / download , Overtime, etc …

Our network request is dio Based on the package . So we have to introduce it dio package .

<> Add dependency
dependencies: dio: ^x.x.x // Please use pub The latest version on
<>HttpUtil
/* * Created by Li Zhuoyuan on 2018/9/15. * email: [email protected] * Network request encapsulation */
import 'package:dio/dio.dart'; class HttpUtil { static HttpUtil instance; Dio
dio; Options options; static HttpUtil getInstance() { print('getInstance'); if
(instance == null) { instance = new HttpUtil(); } return instance; } HttpUtil()
{ print('dio assignment '); // Or by passing a `options` To create dio example options = Options( //
Request base address , Can contain subpaths , as : "https://www.google.com/api/". baseUrl:
"https://www.xx.com/api", // Connection server timeout , The unit is millisecond . connectTimeout: 10000, ///
The interval between before and after receiving data on the response stream , The unit is Ms . If the interval between two times exceeds [receiveTimeout], /// [Dio]
Will throw a [DioErrorType.RECEIVE_TIMEOUT] The abnormality of . /// be careful : This is not the total time limit for receiving data . receiveTimeout:
3000, headers: {}, ); dio = new Dio(options); } get(url, {data, options,
cancelToken}) async { print('get Request to start ! url:$url ,body: $data'); Response
response; try { response = await dio.get( url, data: data, cancelToken:
cancelToken, ); print('get Request successful !response.data:${response.data}'); } on DioError
catch (e) { if (CancelToken.isCancel(e)) { print('get Request cancellation ! ' + e.message); }
print('get An error occurred in the request :$e'); } return response.data; } post(url, {data, options,
cancelToken}) async { print('post Request to start ! url:$url ,body: $data'); Response
response; try { response = await dio.post( url, data: data, cancelToken:
cancelToken, ); print('post Request successful !response.data:${response.data}'); } on DioError
catch (e) { if (CancelToken.isCancel(e)) { print('post Request cancellation ! ' + e.message); }
print('post An error occurred in the request :$e'); } return response.data; } }
<> use

import ‘package:flutter_oschina/utils/HttpUtil.dart’;
Future getData() async { String url = Api.NEWS_LIST; var data = {'pageIndex':
1, 'pageSize': 10}; var response = await HttpUtil().get(url, data: data);
setState(() { ... }); }
<> to update :

This article was written in dio1.x edition , Has now been updated to 2.x edition , Options Medium baseUrl from Options Moved to BaseOptions , Please see below

<>Options

Options Object contains the configuration of the network request , stay 1.x In both instance configuration and single request configuration, the Options
object , This will bring some ambiguity , Sometimes it's confusing for developers , such as Options.baseUrl
Property represents the request base address , In theory, it should only be set in the instance configuration , Should not appear in every configuration ; Another example is Options.path
attribute , It represents the relative path of the request , Should not be in the instance request configuration .2.1 Split the request configuration into three classes :

| Class name | effect |

| -------------- | ----------------------------------------------- |

| BaseOptions | Dio Instance based configuration , By default, the dio All requests for instance take effect |

| Options | Single request configuration , Can be covered BaseOptions Property with the same name in |

| RequestOptions | Final configuration of the request , That's right Option and BaseOptions Consolidated |

in addition , Some new configuration items have been added :

*
cookies: You can add some public cookie

*
receiveDataWhenStatusError: When the response status code is not successful ( as 404) Time , Whether to receive the response content , If it is false, be response.data
Will be for null

*
maxRedirects: Maximum redirection times .