dio_adapter 1.9.0
dio_adapter: ^1.9.0 copied to clipboard
is a Dart package that simplifies HTTP requests using the Dio library. It provides a structured way to handle API CRUD operations with customizable request and response handling, error management, and [...]
example/dio_adapter_example.dart
// Example usage of DioBuilderBase
import 'package:dio_adapter/dio_adapter.dart';
void main() async {
final dioBuilder = DioAdapterBase(
baseUrl: 'https://jsonplaceholder.typicode.com',
connectTimeout: Duration(seconds: 10000),
receiveTimeout: Duration(seconds: 10000),
contentTypeEnum: ContentTypeEnum.applicationJson,
responseTypeEnum: ResponseTypeEnum.json,
customRequestHandler: (options, handler) async {
// Do something before request is sent.
// If you want to resolve the request with custom data,
// you can resolve a `Response` using `handler.resolve(response)`.
// If you want to reject the request with a error message,
// you can reject with a `DioException` using `handler.reject(dioError)`.
return options;
},
customResponseHandler: (response, handler) async {
// Do something with response data.
// If you want to reject the request with a error message,
// you can reject a `DioException` object using `handler.reject(dioError)`.
return response;
},
customErrorHandler: (error, handler) async {
// Do something with response error.
// If you want to resolve the request with some custom data,
// you can resolve a `Response` object using `handler.resolve(response)`.
return error;
},
);
// Example GET request
final getResult = await dioBuilder.get('/posts/1');
getResult.fold(
(error) => print('GET Error: $error'),
(response) => print('GET Response: ${response.data}'),
);
//Example POST request
final postResult = await dioBuilder
.post('/posts', body: {'title': 'foo', 'body': 'bar', 'userId': 1});
postResult.fold(
(error) => print('POST Error: $error'),
(response) => print('POST Response: ${response.data}'),
);
// Example PUT request
final putResult = await dioBuilder.put('/posts/1', body: {
'id': 1,
'title': 'updated title',
'body': 'updated body',
'userId': 1
});
putResult.fold(
(error) => print('PUT Error: $error'),
(response) => print('PUT Response: ${response.data}'),
);
// Example DELETE request
final deleteResult = await dioBuilder.delete('/posts/1');
deleteResult.fold(
(error) => print('DELETE Error: $error'),
(response) => print('DELETE Response: ${response.statusCode}'),
);
}