error_handeler_flutter

Last updated:

0 purchases

error_handeler_flutter Image
error_handeler_flutter Images
Add to Cart

Description:

error handeler flutter

A Flutter Package to provide smooth Api call with All Error and Exception handeled.

while using package's api request call you don't have to worry about any exception which might occured including PlatformException , FormatException , SocketException.
Instead of Exception throw this package focus on returning Exxception as Custom Failure class.
you can use Enum to find out which exception has occured and along with it you get access to default message for those Exception and response body of the api in your UI.

This package also ensure proper network checking before making any APi request for making process fast and improve user experience.
Android Configuration #
On Android, for correct working in release mode, you must add INTERNET & ACCESS_NETWORK_STATE permissions to AndroidManifest.xml, follow the next lines:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Permissions for internet_connection_checker -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application

copied to clipboard
You can call InternetConnectionChecker().hasConnection to get bool Status of Internet Connection Availability, kindly note that this will only return Internet Status not Internet proivder device info like wifi, mobile,etc.
if (!await InternetConnectionChecker().hasConnection) {
CustomSnackbar().showNoInternetSnackbar();
}
copied to clipboard
You can use this to show Alert Dialog or run some code
use of package for API Call #
let's setup project to use HTTP and DIO REST API call

void main() async {
// set whether to use http or Dio, by default it will use HTTP
ErrorHandlerFlutter().init(usehttp: false);

// setup DIO
PackageDio.addInterceptors([]);
PackageDio.setBaseOptions(
// you can set more option inside it
// but here i am setting base url to use for api calls
baseUrl: 'https://66c45adfb026f3cc6ceefd10.mockapi.io'
);

// this will add base options and interceptors to dio client
// must be called to setup dio
PackageDio.setUpDio();


// setup HTTP
// you can define your own http client (optional)
PackageHttp.setupClient(client:http.Client() );
// necessary to pass host (base url) to make http request
PackageHttp.setup(host: '66c45adfb026f3cc6ceefd10.mockapi.io',prefix: '');

runApp(const MyApp());
}

copied to clipboard
Initialize Snackbar after MaterialApp is configured.
@override
Widget build(BuildContext context) {

// Make sure to call init function before using api call from ErrorHandelerFlutter class
// context is needed to show No internet Snackbar,
// Otherwise Snackbar will not appear when device is not connected to internet and api request is made
CustomSnackbar().init(context);

return Scaffold(
appBar: AppBar(
copied to clipboard
Calling APi using ErrorHandelerFlutter class
use ErrorHandelerFlutter().get(url) to make GET request call
and get response as Result class, use Switch statement to iterate through Success or Failure
Below is sample code for how the request are made and how response are manipulated

Future<void> callApi() async {
clear();
final Result response = await ErrorHandlerFlutter.get(
'/data/postdata',
);
// await ErrorHandlerFlutter.post('/data/postdata', body: '');
switch (response) {
case Success(value: dynamic data):

result.value = ErrorHandlerFlutter.useHttp
? (await json.decode(data.body)).toString()
: data.data.toString();
debugPrint('result :$data');
break;
case Failure(error: ErrorResponse resp):
debugPrint('the error occured : ${resp.errorHandelerFlutterEnum.name}');

// Holds message regarding error
defMesg.value = resp.errorResponseHolder.defaultMessage;
customMesg.value = resp.errorResponseHolder.customMessage ?? '';

// give error type
// such as badrequest , InternalServerError,etc..
errorEnum.value = resp.errorHandelerFlutterEnum.name;

// if error caught by package such as statusCode >300
// response body of that request is stored here
responsebody.value = resp.errorResponseHolder.responseBody ?? '';

// pass through enums of failure to customize uses according to failures
switch (resp.errorHandelerFlutterEnum) {
case ErrorHandelerFlutterEnum.badRequestError:
debugPrint(
'the status is 400 , Bad request from client side :resbody:${resp.errorResponseHolder.responseBody}\n mesg :${resp.errorResponseHolder.defaultMessage} ');
break;
case ErrorHandelerFlutterEnum.notFoundError:
debugPrint('404 , Api endpoint not found');
break;
default:
debugPrint(
'Not matched in main cases : ${resp.errorHandelerFlutterEnum.name} ${resp.errorResponseHolder.defaultMessage}');
}
break;
default:
debugPrint('Api Response not matched with any cases ');
}
}
copied to clipboard

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.