dio设置自定义post请求_Flutter Dio简单二次封装和自定义Header
話不多說自己看代碼封裝的比較簡單,比較適合入門學習Dio。
import 'package:dio/dio.dart';
import 'Api.dart';
/*
* 封裝 restful 請求
*
* GET、POST、DELETE、PATCH
* 主要作用為統一處理相關事務:
* - 統一處理請求前綴;
* - 統一打印請求信息;
* - 統一打印響應信息;
* - 統一打印報錯信息;
*/
class DioUtils {
/// global dio object
static Dio dio;
/// default options
static const int CONNECT_TIMEOUT = 10000;
static const int RECEIVE_TIMEOUT = 3000;
static const String TOKEN = "1373a739fd8599909738511f41831623";
/// http request methods
static const String GET = 'get';
static const String POST = 'post';
static const String PUT = 'put';
static const String PATCH = 'patch';
static const String DELETE = 'delete';
/// 創建 dio 實例對象
static Dio createInstance() {
if (dio == null) {
/// 全局屬性:請求前綴、連接超時時間、響應超時時間
var options = BaseOptions(
connectTimeout: 150000,
receiveTimeout: 150000,
responseType: ResponseType.json,
validateStatus: (status) {
// 不使用http狀態碼判斷狀態,使用AdapterInterceptor來處理(適用于標準REST風格)
return true;
},
baseUrl: Api.baseUrlDevelop,
headers: httpHeaders);
dio = new Dio(options);
}
return dio;
}
/// 清空 dio 對象
static clear() {
dio = null;
}
///Get請求
static void getHttp(
String url, {
parameters,
Function(T) onSuccess,
Function(String error) onError,
}) async {
///定義請求參數
parameters = parameters ?? {};
//參數處理
parameters.forEach((key, value) {
if (url.indexOf(key) != -1) {
url = url.replaceAll(':$key', value.toString());
}
});
try {
Response response;
Dio dio = createInstance();
response = await dio.get(url, queryParameters: parameters);
var responseData = response.data;
if (responseData['erroCode'] == 2000) {
if (onSuccess != null) {
onSuccess(responseData['result']);
}
} else {
throw Exception('erroMsg:${responseData['erroMsg']}');
}
print('響應數據:' + response.toString());
} catch (e) {
print('請求出錯:' + e.toString());
onError(e.toString());
}
}
///Post請求
static void postHttp(
String url, {
parameters,
Function(T) onSuccess,
Function(String error) onError,
}) async {
///定義請求參數
parameters = parameters ?? {};
//參數處理
parameters.forEach((key, value) {
if (url.indexOf(key) != -1) {
url = url.replaceAll(':$key', value.toString());
}
});
try {
Response response;
Dio dio = createInstance();
response = await dio.post(url, queryParameters: parameters);
var responseData = response.data;
if (responseData['erroCode'] == 2000) {
if (onSuccess != null) {
onSuccess(responseData['result']);
}
} else {
throw Exception('erroMsg:${responseData['erroMsg']}');
}
print('響應數據:' + response.toString());
} catch (e) {
print('請求出錯:' + e.toString());
onError(e.toString());
}
}
/// request Get、Post 請求
//url 請求鏈接
//parameters 請求參數
//method 請求方式
//onSuccess 成功回調
//onError 失敗回調
static void requestHttp(String url,
{parameters,
method,
Function(T t) onSuccess,
Function(String error) onError}) async {
parameters = parameters ?? {};
method = method ?? 'GET';
if (method == DioUtils.GET) {
getHttp(
url,
parameters: parameters,
onSuccess: (data) {
onSuccess(data);
},
onError: (error) {
onError(error);
},
);
} else if (method == DioUtils.POST) {
postHttp(
url,
parameters: parameters,
onSuccess: (data) {
onSuccess(data);
},
onError: (error) {
onError(error);
},
);
}
}
}
/// 自定義Header
Map httpHeaders = {
'Accept': 'application/json,*/*',
'Content-Type': 'application/json',
'token': DioUtils.TOKEN
};
使用
var param = {
'taskCode ': 'XC202001020003'};
DioUtils.requestHttp(
Api.verificationSurvey,
parameters: param,
method:DioUtils.GET,
onSuccess: (data) {
},
onError: (error) {
},
);
}
總結
以上是生活随笔為你收集整理的dio设置自定义post请求_Flutter Dio简单二次封装和自定义Header的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 民生银行信用卡取现相关问题及解答
- 下一篇: 2016招行腿毛卡是哪几张?怎么办理?