safeCallListStatus<T> method
Future<void>
safeCallListStatus<T>({
- required Either<
FetchFailure, BaseListResponse< response,T> ?> - required dynamic funError(),
- required dynamic funDataServer(),
- dynamic funSuccess()?,
- bool isLoadCache = true,
- String keyCache = "",
- dynamic funDataCache(
- List<
T> ?
- List<
- bool isShowError = true,
Implementation
Future<void> safeCallListStatus<T>(
{required Either<FetchFailure, BaseListResponse<T>?> response,
required Function(FetchFailure) funError,
required Function(FetchData<List<T>>) funDataServer,
Function(FetchData<List<T>>)? funSuccess,
bool isLoadCache = true,
String keyCache = "",
Function(List<T>?)? funDataCache,
bool isShowError = true}) async {
try {
if (funDataCache != null && keyCache.isNotEmpty == true && isLoadCache) {
await safeResponseDataCache<List<T>>(
keyCache: keyCache,
funCallBack: (data) {
isShowError = false;
funDataCache(data);
});
}
if (response.get != null) {
if (response.get?.success == true) {
await safeResponseDataServer<List<T>>(
dataServer: response.get?.data,
funCallBack: (data) async {
if (data?.isNotEmpty == true) {
safeDataCache(
isDataCache: keyCache.isNotEmpty == true && isLoadCache,
funCallBack: () {
saveCacheData(key: keyCache, data: data);
});
final msg = response.get?.message;
if (funSuccess != null) {
funSuccess(FetchData<List<T>>(
data: data, paging: response.get?.meta_data, msg: msg));
}
return funDataServer(FetchData<List<T>>(
data: data, paging: response.get?.meta_data, msg: msg));
} else {
return funDataServer(FetchData<List<T>>(data: []));
}
});
} 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));
}
}