api_widget 1.0.3
api_widget: ^1.0.3 copied to clipboard
A powerful and flexible Flutter widget for handling API requests with built-in loading states, error handling, and retry mechanisms.
API Widget #
A powerful and flexible Flutter widget for handling API requests with built-in loading states, error handling, and retry mechanisms.
Links #
Features #
- 🚀 Support for all HTTP methods (GET, POST, PUT, DELETE, PATCH, MULTIPART)
- 🔄 Built-in loading states and error handling
- 🎨 Customizable UI components
- 🔒 Automatic token management with refresh support
- 🔁 Retry mechanism for failed requests
- 🐛 Debug features (curl command generation)
- 📱 Platform support for iOS, Android, Web, Windows, macOS, and Linux
- 📦 Zero dependencies (except http package)
- ✅ Comprehensive test coverage
- 📚 Detailed documentation and examples
Installation #
Add this to your package's pubspec.yaml
file:
dependencies:
api_widget: ^1.0.3
Getting Started #
Basic Usage #
- Initialize the API configuration:
void main() {
ApiConfig.initialize(
accessToken: 'your_access_token',
timeoutDuration: const Duration(seconds: 30),
loaderWidget: () => const CircularProgressIndicator(),
onLogoutMethod: () {
// Handle logout
},
toastWidget: (context, message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(message)),
);
},
handleResponseStatus: (context, response) {
// Handle specific status codes
},
);
runApp(MyApp());
}
- Use the API widget in your code:
final apiWidget = ApiWidget(
url: 'https://api.example.com/data',
method: HttpMethod.get,
context: context,
);
final response = await apiWidget.sendRequest();
Token Management #
The package includes built-in token management with support for runtime token updates:
// Update the access token at runtime
ApiConfig.updateAccessToken("new_token_here");
This is particularly useful for:
- Token refresh scenarios
- User re-authentication
- Switching between different user sessions
Advanced Features #
Multipart Requests
final apiWidget = ApiWidget(
url: 'https://api.example.com/upload',
method: HttpMethod.multipart,
context: context,
fields: {'key': 'value'},
files: {
'file': await ApiWidget.createMultipartFile(
'file',
filePath,
),
},
);
Custom Headers
final apiWidget = ApiWidget(
url: 'https://api.example.com/data',
method: HttpMethod.get,
context: context,
headers: {'Custom-Header': 'value'},
);
Retry Mechanism
final apiWidget = ApiWidget(
url: 'https://api.example.com/data',
method: HttpMethod.get,
context: context,
retryDelay: const Duration(seconds: 2),
);
API Reference #
ApiConfig #
The main configuration class for the API widget.
Methods
initialize()
: Initialize the API configurationupdateAccessToken()
: Update the access token at runtime
ApiWidget #
The main widget for making API requests.
Constructor Parameters
url
: The API endpoint URLmethod
: The HTTP method to usecontext
: The BuildContextbody
: Optional request bodyheaders
: Optional custom headersfiles
: Optional files for multipart requestsfields
: Optional fields for multipart requestsretryDelay
: Delay between retry attempts
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Author #
- DIGVIJAYSINH CHAUHAN
- GitHub: @Digvijaysinh2204
- Email: [digvijaysinh2204@gmail.com]
Acknowledgments #
- Thanks to the Flutter team for the amazing framework
- Thanks to the http package team for the HTTP client implementation