getDio static method

Dio getDio({
  1. Map<String, String>? headers,
  2. bool showLoadingDialog = true,
  3. bool highTimeout = true,
  4. ResponseType responseType = ResponseType.json,
  5. bool? persistentConnection,
})

Implementation

static Dio getDio({
  Map<String, String>? headers,
  bool showLoadingDialog = true,
  bool highTimeout = true,
  ResponseType responseType = ResponseType.json,
  bool? persistentConnection,
}) {
  headers ??= {
    "Cookie": box.read("cookie") ?? "",
    'Authorization': 'Bearer ${box.read("token")}'
  };
  BaseOptions options = BaseOptions(
    persistentConnection: persistentConnection,
    baseUrl: serverUrl,
    connectTimeout: Duration(seconds: highTimeout ? 600 : 30),
    receiveTimeout: Duration(seconds: highTimeout ? 600 : 25),
    responseType: responseType,
    validateStatus: (status) {

      if (status == 401) {

        if (currentRoute() != Routes.SIGNIN) {
          FirebaseAuth.instance.signOut().then((val) {
            offUntilLogin();
          });
          if (currentRoute() != Routes.SIGNIN) {
            CustomFlashWidget.showFlashMessage(
              type: FlashType.error,
              title: "Session Expired",
              message:
              "Your app session has been expired. Please log in again!",
            );
          }
        }
      }
      if (status == 502) {
        CustomFlashWidget.showFlashMessage(
          type: FlashType.error,
          title: "Internal Server Error",
          message: "Please try again later!",
        );
      }
      return (status ?? 0) <= 500;
    },
    headers: headers,
  );
  Dio d = Dio(options);

   var adapter = HttpClientAdapter() as BrowserHttpClientAdapter;
 // var adapter = BrowserHttpClientAdapter();
  d.httpClientAdapter = adapter;
  d.interceptors.add(InterceptorsWrapper(
    onRequest:
        (RequestOptions options, RequestInterceptorHandler handler) async {
      print(options.path);
      if (showLoadingDialog) {
        loadingDialog();
      }
      return handler.next(options);
    },
    onResponse: (DioResponse.Response response,
        ResponseInterceptorHandler handler) async {
      if (showLoadingDialog) {
        closeDialog();
      }
      if (kDebugMode && response.statusCode != 200) {
        print(response.data);
      }
      return handler.next(response); // continue
    },
    onError: (DioError e, ErrorInterceptorHandler handler) async {
      if (showLoadingDialog) {
        closeDialog();
      }
      if (kDebugMode) {
        print(e.response?.data);
      }
      return handler.next(e); //continue
    },
  ));

  return d;
}