HttpHandler is a Flutter package designed to simplify network API calls using the HTTP client. It provides utility functions and customization options to streamline API request handling in your Flutter applications.
- Simplified API calls with Http.
- Customizable error and loading dialogs.
- Network connectivity check.
- Callback time measurement.
To use HttpHandler in your Flutter project, add it as a dependency in your pubspec.yaml
file:
dependencies:
http_handler: <latest_version>
Then, run flutter pub get
to install the package.
First, import the http_handler
package in your Dart file:
import 'package:http_handler/http_handler.dart';
Create an instance of HttpHandler
by providing a http instance and other optional parameters:
final httpHandler = HttpHandler(
getBuildContext: context, // Required: Provide context
customErrorDialog: MyCustomErrorDialog(), // Optional: Provide a custom error dialog widget
customLoadingDialog: MyCustomLoadingDialog(), // Optional: Provide a custom loading dialog widget
isCheckNetworkConnectivity: true, // Optional: Enable network connectivity check
isAlertDialogs: true, // Optional: Show alert dialogs for errors
isCallBackTime: true, // Optional: Measure API callback time (debug mode)
);
Replace MyCustomErrorDialog()
and MyCustomLoadingDialog()
with your actual custom error and loading dialog widgets if needed.
To make an API request and handle responses, use the callAPI()
method:
httpHandler.callAPI(
serviceUrl: 'https://api.example.com/data',
method: 'GET',
success: (response) {
// Handle successful response here
print('API Request Successful');
print(response.data);
},
error: (error) {
// Handle error response here
print('API Request Error');
print(error);
},
showProcess: true, // Set to true to display a loading dialog
);
You can find an example of how to use HttpHandler in the example
folder of this package. To run the example, follow these steps:
- Clone this repository.
- Navigate to the
example
folder. - Run
flutter run
to launch the example app.
If you have any questions, feel free to reach out:
- Email: chhapawalaarfaz@gmail.com
- GitHub: @Arfaz123
- LinkedIn: @Arfaz Chhapawala
- YouTube: @devfaaz
Contributions to the http_handler
package are welcome! If you find a bug or want to add a new feature, feel free to open an issue or submit a pull request.
This package is released under the MIT License. See the LICENSE file for details.
Feel free to explore the features of the HttpHandler package and customize it to suit your needs. If you have any questions or feedback, don't hesitate to reach out. Happy coding!