Based on the latest version of the latest Flutter teaching grade Dio package

Nek.Dart

import 'dart:convert'; import 'package:becoin/Routers/Routes.dart'; import 'package:common_utils/common_utils.dart'; import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; Import 'package:flutter_easyloading/flutter_easyloading.dart'; Class Request {static BaseOptions _options = BaseOptions(///Api address baseUrl: 'https://www.beacons.vip', /// open timeout duration connectTimeout: 50000, /// receiveTimeout duration receiveTimeout: 30000,); Static Dio _dio = Dio(_options); Static Future<T> _request<T>(String path,{String? method, Map? Params, data}) async {// async (params! = null) { params.forEach((key, value) { if (path.indexOf(key) ! = -1) { path = path.replaceAll(":$key", value.toString()); }}); } logutil. v(data, tag: 'sent data: '); Try {/// The loading popover is redefined here, or you can use your own easyload.instance.. LoadingStyle = EasyLoadingStyle. Custom BackgroundColor = Color(0xffF4f7fb) // progress Color.. indicatorColor = Color(0xff0082CD) .. textColor = Color(0xff0082CD) .. textStyle = TextStyle(fontSize: 12) .. indicatorType = EasyLoadingIndicatorType.wave; // execute the loading animation easyloading.show (); Response response = await _dio.request(path,data: data, options: Options(method: method,contentType: Headers.formUrlEncodedContentType,)); If (response. StatusCode = = 200 | | response. The statusCode = = 201) {/ / / if the request to close the popup window animation EasyLoading. Dismiss (); Var data = jsonDecode(response.data); try { // return data; If (data['code'] == "1") {logutil. v(data['code'],tag: 'server error, status code: '); Easyload. showInfo(" ${data[' MSG ']}"); return Future.error(data['msg']); } else if(data['code'] == "404"){/// If the state is lost, the user token data is empty, so that the boot page can directly log in to easyLoading.showError (" the current state is lost, please log in again ".tr); Return future. error(data[' MSG ']); }else {/// Other status Description Normal logutil. v(data,tag: 'Response data: '); return data; }} catch (e) {logutil. v(e, tag: 'error 1'); Return future. error(' parse response data exception 2'); }} else {logutil. v(response.statuscode, tag: 'HTTP error, statusCode: '); Easyload.showinfo ('HTTP error, statusCode: ${response.statuscode}'); _handleHttpError(response.statusCode!) ; Return future. error('HTTP error '); }} on DioError catch (e, s) {logutil. v(_dioError(e), tag: 'DioError '); EasyLoading.showInfo(_dioError(e)); return Future.error(_dioError(e)); } catch (e, s) {logutil. v(e, tag: 'unknown exception '); Return future. error(' unknown exception '); }} / / Dio exception handling static String _dioError (DioError error) {switch (error. A type) {case DioErrorType. ConnectTimeout: Return "Network connection timed out, please check network Settings "; break; Abnormal case DioErrorType. ReceiveTimeout: return "server, please try again later!" ; break; Case DioErrorType. SendTimeout: return "network connection timeout, please check the network Settings". break; Case DioerrorType. response: return "Server exception, please try again later!" ; break; Case DioerrorType. cancel: return "Request has been canceled, please request again "; break; Case DioErrorType. Other: return "Network error, please try again later!" ; break; Default: return "Dio exception "; Static void _handleHttpError(int errorCode) {String message; Switch (errorCode) {case 400: message = 'Request syntax error '; break; Case 401: message = 'not authorized, please login '; break; Case 403: message = 'access denied '; break; Case 404: message = 'error '; break; Case 408: message = 'request timed out '; break; Case 500: message = 'error '; break; Case 501: message = 'service not implemented '; break; Case 502: message = 'gateway error '; break; Case 503: message = 'service unavailable '; break; Case 504: message = 'gateway timed out '; break; Case 505: message = 'HTTP version not supported '; break; Default: message = 'request failed, errorCode: $errorCode'; } EasyLoading.showError(message); } static Future<T> get<T>(String path, {Map? params}) { return _request(path, method: 'get', params: params); } static Future<T> post<T>(String path, {Map? params, data}) { return _request(path, method: 'post', params: params, data: data); } // Get and post are used as aliases.Copy the code

Api.Dart

import 'Nek.dart'; Static code(data) {return request. post("/user/code",data: data,); }}Copy the code

How to eat it

var info = await Api.code(data);
Copy the code