dioGet function

Future<Response?> dioGet(
  1. String endUrl, {
  2. bool useBaseUrl = true,
  3. String? loginRouteName,
})

Perform a GET request

Implementation

Future<DIO.Response<dynamic>?> dioGet(
  String endUrl, {
  bool useBaseUrl = true,
  String? loginRouteName,
}) async {
  var dio = DIO.Dio();

  // Configure Dio to allow insecure connections
  (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
      (client) {
    client.badCertificateCallback =
        (X509Certificate cert, String host, int port) {
      // Allow insecure connections
      return true;
    };
    return client;
  };

  final storage = GetStorage();

  if (storage.read(USER_LOGIN) ?? false) {
    dio.options.headers['Authorization'] =
        "Bearer ${storage.read(USER_TOKEN) ?? ''}";
  }

  try {
    var url = useBaseUrl ? "$BASE_URL$endUrl" : endUrl;
    var response = await dio.get(
      url,
      options: DIO.Options(
        validateStatus: (status) => true,
        sendTimeout: const Duration(milliseconds: 100000),
        receiveTimeout: const Duration(milliseconds: 150000),
      ),
    );

    _handleStoreStatus(response);

    if (!response.data["error"] && _shouldNavigateToLogin(response)) {
      await _clearUserData();
      if (loginRouteName != null && Get.isRegistered<GetDelegate>()) {
        Get.offAllNamed(loginRouteName);
      }
      return null;
    }

    if (isDebugMode.value) {
      log("\n\nGET: $url\nSTATUS CODE: ${response.statusCode}\n${jsonEncode(response.data)}\n\n");
    }

    return response;
  } catch (e) {
    if (isDebugMode.value) {
      log("Error in GET request: $e");
    }
    return null;
  }
}