network 0.4.0 network: ^0.4.0 copied to clipboard
Package including hooks for easy works with http package in dart
Package including hooks for easy works with http package in dart.
Key Features • Getting Started • Todo • Credits
Key Features #
- Simple hooks for get / post
- Specify response type (blob\json api)
Getting Started #
Get request to API:
main() async {
final getResponse = await network.get<network.JsonApiResponse>(
'https://jsonplaceholder.typicode.com/todos/1');
print(getResponse.toMap['title']);
}
Get request Blob:
main() async {
final blobResponse = await network.get(
'https://via.placeholder.com/300');
print(blobResponse.bytes);
}
Post request to API:
final postResponse = await network.post<network.JsonApiResponse>(
'https://jsonplaceholder.typicode.com/todos',
body: {'title': 'test'});
print(postResponse.toMap['id']);
Handle exceptions:
try {
await network.get('https://jsonplaceholder.typicode.com/todos/202');
} on network.NetworkException catch (error) {
print('Network exception called, status code: ${error.code}');
}
Provide their exceptions:
network.NetworkSettings().exceptionDelegate = (network.NetworkException error) {
// You can check type of respose in error by:
// if (error is network.JsonApiResponse)
switch (error.code) {
case 400:
throw BadRequestException(error.response);
case 404:
throw NotFoundException(error.response);
default:
throw error;
}
};
And... api reference available here
TODO #
- ✅ Get
- ✅ Post
- ✅ Delete
- ✅ Put
- ❌ Decorators (#1)
Credits #
This software uses the following open source packages: