api_widget 1.0.3 copy "api_widget: ^1.0.3" to clipboard
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.

pub package License: MIT GitHub issues GitHub pull requests

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 #

  1. 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());
}
  1. 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 configuration
  • updateAccessToken(): Update the access token at runtime

ApiWidget #

The main widget for making API requests.

Constructor Parameters

  • url: The API endpoint URL
  • method: The HTTP method to use
  • context: The BuildContext
  • body: Optional request body
  • headers: Optional custom headers
  • files: Optional files for multipart requests
  • fields: Optional fields for multipart requests
  • retryDelay: 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 #

Acknowledgments #

  • Thanks to the Flutter team for the amazing framework
  • Thanks to the http package team for the HTTP client implementation
3
likes
160
points
86
downloads

Publisher

unverified uploader

Weekly Downloads

A powerful and flexible Flutter widget for handling API requests with built-in loading states, error handling, and retry mechanisms.

Repository (GitHub)
View/report issues

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

flutter, http

More

Packages that depend on api_widget