easy_api_provider 2.0.0
easy_api_provider: ^2.0.0 copied to clipboard
A simple and customizable API provider using Dio for network requests in Flutter.
easy_api_provider #
A Flutter package providing a Dio-based HTTP client with built-in UI state management. Make API requests easily with automatic error handling and reactive UI updates.
Features #
- Simple and easy-to-use API request handling with Dio
- Customizable headers, timeouts, and query parameters
- Support for GET, POST, PUT, DELETE, PATCH, and DOWNLOAD
- Built-in UI state management (idle, loading, success, error, empty)
- Automatic request/response error handling — never throws exceptions
- Request/response logging with TalkerDioLogger
- Smooth animated transitions between UI states
Requirements #
- Dart SDK
>=3.0.0 - Flutter >= 3.0.0
Installation #
Add the following line to your pubspec.yaml file:
dependencies:
easy_api_provider: ^2.0.0
Then, run:
flutter pub get
Usage #
Import the package #
import 'package:easy_api_provider/easy_api_provider.dart';
Initialize the provider #
void main() {
ApiProvider.instance.init(ApiProviderConfig(
'https://example.com/api',
maxRedirects: 1,
contentType: 'application/json',
receiveTimeout: const Duration(seconds: 30),
connectTimeout: const Duration(seconds: 30),
responseType: ResponseType.json,
headers: {
'Accept': 'application/json'
},
onResponse: (Response response) {},
onError: (DioException error) {},
onRequest: (RequestOptions options) {},
authorization: 'Bearer <Your bearer token>',
extra: {
'key': 'value'
},
listFormat: ListFormat.multi,
requestLogger: true,
));
runApp(const MyApp());
}
Making a GET request #
Future<void> fetchData() async {
final ApiResponse response = await ApiProvider.instance.get(
'/example',
params: {
'param1': 'value1',
},
progressCallback: (int c, int s) {},
requestOptions: Options(),
);
if (response.success) {
// Handle success
} else {
// Handle error
}
}
Making a POST request #
Future<void> sendData() async {
final ApiResponse response = await ApiProvider.instance.post(
'/example',
data: {'key': 'value'},
params: {
'param1': 'value1',
},
onSendProgress: (int c, int s) {},
onReceiveProgress: (int c, int s) {},
requestOptions: Options(),
);
}
ApiResponse Class #
The ApiResponse class encapsulates the results of network requests executed using Dio.
Properties #
| Property | Type | Description |
|---|---|---|
success |
bool |
Whether the request was successful |
statusCode |
int? |
HTTP status code (200, 400, 500, etc.) |
data |
dynamic |
Response data on success, error details on failure |
url |
String? |
The request URL |
message |
String? |
Descriptive message for debugging |
Using the UI Handler #
The ApiProviderUi widget dynamically switches UI based on the API request status managed by ApiProviderController.
States #
| State | Description |
|---|---|
idle |
Initial state before any API request |
loading |
Request is in progress |
success |
Request completed successfully |
error |
Request resulted in an error |
empty |
Successful response with no data |
Setup #
final controller = ApiProviderController();
ApiProviderUi(
controller: controller,
idleWidget: (context) => const IdleWidget(),
loadingWidget: (context) => const LoadingWidget(),
emptyWidget: (context) => const EmptyWidget(),
successWidget: (context, response) => SuccessWidget(response),
errorWidget: (context, response) => ErrorWidget(response),
)
Manual state control #
controller.idle();
controller.loading();
controller.empty();
controller.success();
controller.error();
Listen for status changes #
controller.listen((ApiProviderStatus status) {
// ApiProviderStatus.idle, .loading, .success, .error, .empty
});
Automatic state management #
Pass a controller to any request method and the state is managed automatically:
final ApiResponse response = await ApiProvider.instance.get(
'/users',
controller: controller,
);
This automatically:
- Sets loading state before sending the request
- Sets success state with the
ApiResponseon success - Sets error state with the
ApiResponseon failure
Contributing #
Contributions are welcome! Feel free to open issues or submit pull requests.
License #
This package is released under the MIT License. See LICENSE for details.