Android | iOS | macOS | Web | Linux | Windows |
---|---|---|---|---|---|
✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
The Flutter API Client is a package designed to simplify the process of making API requests in Flutter projects. It is built on top of the popular dio
package, providing a streamlined and efficient way to communicate with RESTful APIs.
A powerful HTTP networking package for Dart/Flutter, supports Global configuration, Interceptors, FormData, Request cancellation, File uploading/downloading, Timeout, Custom adapters, Transformers, etc.
- Easy integration: The Flutter API Client is easy to integrate into your Flutter projects, allowing you to start making API requests quickly.
- Simple configuration: The package provides a straightforward configuration process, making it easy to set up and customize your API client.
- Support for authentication: The Flutter API Client supports various authentication methods, including token-based authentication and API key authentication.
- Flexible request options: With the Flutter API Client, you can easily customize request options such as headers, query parameters, timeouts, and more.
- Error handling: The package includes error handling mechanisms to handle API errors gracefully and provide meaningful error messages.
To use the Flutter API Client in your Flutter project, add the following dependency to your pubspec.yaml
file:
dependencies:
dio_api_client: ^5.3.3
Then, run the command flutter pub get
to fetch the package.
- Import the package:
import 'package:dio_api_client/dio_api_client.dart';
- Initialize the API client:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final directory = await getTemporaryDirectory();
await DioApiClient.init(directory);
runApp(const MyApp());
}
- Make API class and extend
DioApiClient
:
/// * [T] is the type of the response data
class PubPackage extends DioApiClient<T> {
@override
Duration get cacheDuration => Duration.zero;
@override
String get endpoint => 'pub.dev/api/';
@override
String get path => 'packages/';
Future<T?> getPackage(String name) async {
final response = await get(
dynamicPath: name,
);
return response.data;
}
}
You can configure the API client by overriding the following methods:
/// * [T] is the type of the response data
class MainBase<T> extends DioApiClient<T> {
@override
List<Interceptor> get interceptors => [authInterceptor, logInterceptor];
@override
Duration get cacheDuration => Duration.zero;
@override
String get endpoint => 'pub.dev/api/';
@override
String get path => 'packages/';
}
class PubPackage extends MainBase<T> {
Future<T?> getPackage(String name) async {
final response = await get(
dynamicPath: name,
);
return response.data;
}
}
Contributions are welcome! If you encounter any issues or have suggestions for improvements, please open an issue. Additionally, feel free to submit pull requests with bug fixes or new features.
When contributing to this project, please follow the code of conduct.
This package is open source and released under the MIT License. Feel free to use, modify, and distribute the package according to the terms of the license.
If you have any questions or inquiries about the Flutter API Client, please contact contact@mohesu.com.
Happy coding! 💙 Flutter