safeCallObjectStatus<T> method
Future<void>
safeCallObjectStatus<T>({
- required Either<
FetchFailure, BaseObjectResponse< response,T?> > - required dynamic funError(),
- required dynamic funDataServer(
- FetchData<
T>
- FetchData<
- dynamic funSuccess(
- FetchData<
T>
- FetchData<
- String keyCache = "",
- dynamic funDataCache(
- T?
- bool isShowError = true,
Implementation
Future<void> safeCallObjectStatus<T>(
{required Either<FetchFailure, BaseObjectResponse<T?>> response,
required Function(FetchFailure) funError,
required Function(FetchData<T>) funDataServer,
Function(FetchData<T>)? funSuccess,
String keyCache = "",
Function(T?)? funDataCache,
bool isShowError = true}) async {
try {
if (funDataCache != null && keyCache.isNotEmpty == true) {
await safeResponseDataCache<T>(
keyCache: keyCache,
funCallBack: (data) {
isShowError = false;
funDataCache(data);
});
}
if (response.get != null) {
if (response.get?.success == true) {
safeResponseDataServer<T>(
dataServer: response.get?.data,
funCallBack: (data) async {
if (data != null) {
safeDataCache(
isDataCache: keyCache.isNotEmpty == true,
funCallBack: () {
saveCacheData(key: keyCache, data: data);
});
final msg = response.get?.message;
final codeName = response.get?.code_name;
if (funSuccess != null) {
funSuccess(
FetchData<T>(data: data, msg: msg, codeName: codeName));
}
return funDataServer(
FetchData<T>(data: data, msg: msg, codeName: codeName));
} else {
return funError(await safeResponseError(
typeError: TypeError.ERROR_NO_DATA,
isShowError: isShowError));
}
});
} else {
final msg = response.get?.message.toString() ?? "Error";
final data = response.get?.data.toString() ?? "Error";
return funError(await safeResponseError(
typeError: TypeError.ERROR_WARNING,
errorServer: FetchFailure.warningError(200, [msg], data),
isShowError: isShowError));
}
} else {
return funError(await safeResponseError(
typeError: TypeError.ERROR_SERVER,
errorServer: response.leftOrCrash,
isShowError: isShowError));
}
} catch (e) {
final failure = returnFailure(e);
return funError(await safeResponseError(
typeError: TypeError.ERROR_UNKNOWN,
errorServer: FetchFailure.crashError(100, failure, "Error"),
isShowError: isShowError));
}
}