getDio static method

Dio getDio({
  1. Map<String, String>? headers,
  2. bool showLoadingDialog = true,
})

Implementation

static Dio getDio(
    {Map<String, String>? headers, bool showLoadingDialog = true}) {
  print("token: ${box.read("token")}");
  headers ??= {
    "Accept": "application/json",
    "Content-Type": "application/json",
    "Authorization": "Bearer ${box.read("token") ?? ""}",
  };

  BaseOptions options = BaseOptions(
    baseUrl: serverUrl,
    connectTimeout: const Duration(milliseconds: 30000),
    receiveTimeout: const Duration(milliseconds: 25000),
    validateStatus: (status) {
      if (status! == 401 || status == 403) {
        FirebaseAuth.instance.signOut().then((val) {
          if (Get.currentRoute != Routes.LOGIN) {
            Get.offAllNamed(Routes.LOGIN);
          }
        });
        if (Get.currentRoute != Routes.LOGIN) {
          Get.snackbar("Session Expired".tr,
              "Your app session has been expired. Please log in again!".tr);
        }
      }
      if (status == 502) {
        Get.snackbar(
            "Internal Server Error".tr, "Please try again later!".tr);
      }
      return status <= 500;
    },
    headers: headers,
  );
  Dio d = Dio(options);
  var adapter = BrowserHttpClientAdapter();
  d.httpClientAdapter = adapter;
  d.interceptors.add(InterceptorsWrapper(
    onRequest:
        (RequestOptions options, RequestInterceptorHandler handler) async {
      if (kDebugMode) {
        print('Request Path: ${options.path}');
      }
      var token = box.read("token");
      if (token == null) {
        FirebaseAuth.instance.signOut().then((val) {
          if (Get.currentRoute != Routes.LOGIN) {
            Get.offAllNamed(Routes.LOGIN);
          }
        });
        //qrb-1030
        // if (Get.currentRoute != Routes.LOGIN) {
        //   Get.snackbar("Token not Found".tr,
        //       "Can't find session token. Please log in again!".tr);
        // }
      }

      if (!(Get.isDialogOpen ?? false) && showLoadingDialog) {
        Get.dialog(
          barrierColor: ColorHelper.loaderBarrierColor,
          const LoadingDialog()
        );
      }
      return handler.next(options); //continue
    },
    onResponse: (response.Response response,
        ResponseInterceptorHandler handler) async {
      if ((Get.isDialogOpen ?? false) && showLoadingDialog) {
        Navigator.of(Get.overlayContext!).pop();
      }
      if (response.statusCode != 200) {
        if (kDebugMode) {
          print('Response Status Code: ${response.statusCode}');
          print('Response Data: ${response.data}');
        }
      }
      return handler.next(response); // continue
    },
    onError: (DioException e, ErrorInterceptorHandler handler) async {
      if ((Get.isDialogOpen ?? false) && showLoadingDialog) {
        Navigator.of(Get.overlayContext!).pop();
      }
      if (kDebugMode) {
        print('Error Type: ${e.type}');
        print('Error Message: ${e.message}');
      }
      return handler.next(e); //continue
    },
  ));

  return d;
}