prepareDioFunction method
- {BuildContext? context,
- required DioExceptionType type,
- required DioExceptionHandlerModel? exceptionHandlers}
Prepares a DioException handling function based on the provided context and exception type.
context
is the current build context.type
is the type of DioException.exceptionHandlers
contains a set of custom Dio exception handling functions.generalFunction
is a function to handle exceptions when a specific function is not provided, andcontext
is null. It is only invoked in this case.
Returns a function to handle the specific DioException.
Implementation
bool Function(DioException exception)? prepareDioFunction({
BuildContext? context,
required DioExceptionType type,
required DioExceptionHandlerModel? exceptionHandlers,
}) {
if (context == null) {
return (DioException exception) {
debugPrint(exception.message);
return true;
};
}
switch (type) {
// Handle connection timeout exception.
case DioExceptionType.connectionTimeout:
return (DioException exception) => showAlertDialog(
context,
exceptionHandlers?.connectionTimeoutModel?.title ??
"Connection Timeout",
exceptionHandlers?.connectionTimeoutModel?.content ??
"The connection timed out. Please try again.",
exceptionHandlers?.connectionTimeoutModel?.buttonContent ??
"Close",
);
// Handle send timeout exception.
case DioExceptionType.sendTimeout:
return (DioException exception) => showAlertDialog(
context,
exceptionHandlers?.sendTimeoutModel?.title ?? "Send Timeout",
exceptionHandlers?.sendTimeoutModel?.content ??
"Sending data timed out. Please try again.",
exceptionHandlers?.sendTimeoutModel?.buttonContent ?? "Close",
);
// Handle receive timeout exception.
case DioExceptionType.receiveTimeout:
return (DioException exception) => showAlertDialog(
context,
exceptionHandlers?.receiveTimeoutModel?.title ??
"Receive Timeout",
exceptionHandlers?.receiveTimeoutModel?.content ??
"Receiving data timed out. Please try again.",
exceptionHandlers?.receiveTimeoutModel?.buttonContent ?? "Close",
);
// Handle invalid certificate exception.
case DioExceptionType.badCertificate:
return (DioException exception) => showAlertDialog(
context,
exceptionHandlers?.badCertificateModel?.title ??
"Invalid Certificate",
exceptionHandlers?.badCertificateModel?.content ??
"The server's certificate is invalid. Please connect to a secure network for communication.",
exceptionHandlers?.badCertificateModel?.buttonContent ?? "Close",
);
// Handle bad response exception.
case DioExceptionType.badResponse:
return (DioException exception) => showAlertDialog(
context,
exceptionHandlers?.badResponseModel?.title ?? "Bad Response",
exceptionHandlers?.badResponseModel?.content ??
"A bad response was received from the server.",
exceptionHandlers?.badResponseModel?.buttonContent ?? "Close",
);
// Handle cancelled exception.
case DioExceptionType.cancel:
return (DioException exception) => showAlertDialog(
context,
exceptionHandlers?.cancelModel?.title ?? "Cancelled",
exceptionHandlers?.cancelModel?.content ??
"The operation was cancelled.",
exceptionHandlers?.cancelModel?.buttonContent ?? "Close",
);
// Handle connection error exception.
case DioExceptionType.connectionError:
return (DioException exception) => showAlertDialog(
context,
exceptionHandlers?.connectionErrorModel?.title ??
"Connection Error",
exceptionHandlers?.connectionErrorModel?.content ??
"A connection error occurred. Please check your network connection.",
exceptionHandlers?.connectionErrorModel?.buttonContent ?? "Close",
);
// Handle unknown exception.
case DioExceptionType.unknown:
return (DioException exception) => showAlertDialog(
context,
exceptionHandlers?.unknownModel?.title ?? "Unknown Error",
exceptionHandlers?.unknownModel?.content ??
"An unexpected error occurred.",
exceptionHandlers?.unknownModel?.buttonContent ?? "Close",
);
}
}