doGet static method

Future<FastHttpResponse> doGet({
  1. required String url,
  2. Map<String, dynamic>? body,
  3. Map<String, dynamic> headers = const {},
  4. bool isReconnectStrategyStart = false,
  5. int reconnectTime = 0,
  6. String? hud,
})

Implementation

static Future<FastHttpResponse> doGet({
  required String url,
  Map<String, dynamic>? body,
  Map<String, dynamic> headers = const {},
  bool isReconnectStrategyStart = false,
  int reconnectTime = 0,
  String? hud,
}) async {
  adio.BaseOptions options = adio.BaseOptions(
    connectTimeout: CONNECT_TIMEOUT,
    receiveTimeout: RECEIVE_TIMEOUT,
    baseUrl: fastDio?.options.baseUrl ?? '',
  );

  options.headers = fastDio?.options.headers;

  Map? result;
  Dio dio = fastDio ?? Dio(options);
  FastHttpResponse httpResponse;

  //如果要提示加载中之类的hud
  if (hud != null && hud.isNotEmpty) {
    EasyLoading.show(status: hud);
  }

  adio.Response response = await dio
      .get(
    url,
    options: adio.Options(headers: headers),
    queryParameters: body,
  )
      .catchError((e) async {
        print("error =====>$e");
    DioError error = e;
    if (reconnectTime > 0 &&
        isReconnectStrategyStart &&
        (error.type == DioErrorType.sendTimeout ||
            error.type == DioErrorType.receiveTimeout ||
            error.type == DioErrorType.connectTimeout)) {
      print('start reconnect reconnectTime left: $reconnectTime');
      httpResponse = await doGet(
        url: url,
        body: body,
        headers: headers,
        isReconnectStrategyStart: isReconnectStrategyStart,
        reconnectTime: reconnectTime--,
      );
      // ignore: invalid_return_type_for_catch_error
      return httpResponse;
    } else {
      result = {
        "msg": "Server busy,please try again later",
        "code": -1,
        "msgCode": "1"
      };
      httpResponse = FastHttpResponse(jsonEncode(result), -1, {}, result);
      // ignore: invalid_return_type_for_catch_error
      return httpResponse;
    }
  });

  if (hud != null && hud.isNotEmpty) {
    EasyLoading.dismiss();
  }

  if (response.statusCode == HttpStatus.ok) {
    var data = response.data;

    if (data is String) {
      result = jsonDecode(data);
    } else if (data is Map) {
      result = data;
    }
  } else {
    result = {
      "code": response.statusCode,
      "msg": response.statusMessage,
      "data": ""
    };
    if (fastDio != null) {
      throw Exception(result); //新版本 兼容旧版本
    }
  }

  httpResponse = FastHttpResponse(jsonEncode(result), response.statusCode,
      response.headers.map, result);

  return httpResponse;
}