dio_builder 3.0.1 copy "dio_builder: ^3.0.1" to clipboard
dio_builder: ^3.0.1 copied to clipboard

Dio builder is support plugin using dio, it helps to easy config dio in your project.

# DioBuilder

DioBuilder is a Dart library that provides a builder pattern for configuring and creating instances of the Dio HTTP client. It abstracts common configurations and interceptors for making HTTP requests, making it easier to set up and use Dio for your network requests.

## Installation

Add `dio_builder` to your `pubspec.yaml` file:

```yaml
dependencies:
  dio_builder: ^3.0.0 # Replace with the latest version

Then, run flutter pub get to install the dependency.

Usage #

  1. Import the necessary libraries:
import 'package:dio_builder/dio_builder.dart';
import 'package:dio/dio.dart';
  1. Initialize the DioBuilder:
final dioBuilder = DioBuilder();
dioBuilder.initialize();
  1. Configure and build your Dio instance:
final dio = dioBuilder
    .setBaseUrl('https://api.example.com')
    .setContentType('application/json')
    .setHeaders({'Authorization': 'Bearer YOUR_ACCESS_TOKEN'})
    .build();
  1. Use the Dio instance to make HTTP requests:
try {
  final response = await dio.get('/some-endpoint');
  // Handle the response
} catch (e) {
  // Handle errors
}

Configuration #

DioBuilder provides various methods to configure your Dio instance:

  • setBaseUrl: Set the base URL for your API.
  • setContentType: Set the content type for requests.
  • setHeaders: Set custom headers for requests.
  • setFollowRedirects: Enable or disable following redirects.
  • setExtras: Set extra options for requests.
  • setQueryParameters: Set query parameters for requests.
  • setListFormat: Set the list format for serialization.
  • setMethod: Set the HTTP method for requests.
  • setDefaultTimeOut: Set the default timeout for network requests.
  • setResponseType: Set the response type for requests.
  • setPersistentConnection: Enable or disable persistent connections.
  • setMaxRedirects: Set the maximum number of redirects to follow.
  • setReceiveDataWhenStatusError: Set whether to receive data when the status is an error.

You can chain these methods together to configure DioBuilder according to your requirements.

Interceptors #

DioBuilder includes some common interceptors by default:

  • Logger Interceptor: Logs HTTP requests and responses.
  • Retry Interceptor: Automatically retries failed requests.

You can add or remove interceptors using the addLogger and allowRetryInFailed methods.

Examples #

Here are some examples of making different types of requests using DioBuilder:

GET Request #

final response = await dioBuilder.get(
  endPoint: '/get-endpoint',
  data: {'param1': 'value1'},
);

POST Request #

final response = await dioBuilder.post(
  endPoint: '/post-endpoint',
  data: {'param1': 'value1'},
);

PUT Request #

final response = await dioBuilder.put(
  endPoint: '/put-endpoint',
  data: {'param1': 'value1'},
);

DELETE Request #

final response = await dioBuilder.delete(
  endPoint: '/delete-endpoint',
  data: {'param1': 'value1'},
);

Handling Responses #

You can access the response data using response.data. For example:

final responseData = response.data;

Handling Errors #

If an error occurs during the request, it will be caught in the catch block. For example:

try {
  final response = await dioBuilder.get(
    endPoint: '/non-existent-endpoint',
    data: {'param1': 'value1'},
  );
} catch (e) {
  // Handle the error
}

Contributing #

Contributions to DioBuilder are welcome! If you encounter any bugs or have suggestions for improvements, please create a new issue on the GitHub repository.

If you would like to contribute code to DioBuilder, please follow the contribution guidelines.

License #

DioBuilder is released under the MIT License. See the LICENSE file for more details.

0
likes
120
pub points
2%
popularity

Publisher

unverified uploader

Dio builder is support plugin using dio, it helps to easy config dio in your project.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

chucker_flutter, dio, dio_http2_adapter, dio_smart_retry, flutter, pretty_dio_logger

More

Packages that depend on dio_builder