network

Last updated:

0 purchases

network Image
network Images
Add to Cart

Description:

network

Package including hooks for easy works with http package in dart.






Key Features •
Getting Started •
Todo •
Credits

Key Features #

Simple hooks for all http methods
Interceptors witch handling requests, responses and errors
Decode json from response (Response.json() / Response.asMap / Response.asList)
Browser support

Getting Started #
import 'package:network/network.dart';

// hooks
'https://jsonplaceholder.typicode.com/comments'.get(...);
'https://jsonplaceholder.typicode.com/comments'.post(...);

// Client
final client = NetworkClient();
client.get(...);
client.post(...);
client.close();
copied to clipboard
Get request:
final restResponse = await 'https://jsonplaceholder.typicode.com/todos/1'.get(
queryParameters: {'name': 'value'}
);
// Auto decode json response
print(restResponse.asMap['title']);
// Also available lists
print(restResponse.asList[0]);
// Or object
print(restResponse.json()); // returns Object
// Or pure string
print(restResponse.asString);
copied to clipboard
Get request Blob:
final blobResponse = await 'https://via.placeholder.com/300'.get();
print(blobResponse.bytes);
copied to clipboard
Post request to API:
final postResponse = await 'https://jsonplaceholder.typicode.com/todos'.post(
body: {'title': 'test'},
);
print(postResponse.asMap['id']);
copied to clipboard
Handle exceptions:
try {
await 'https://jsonplaceholder.typicode.com/todos/202'.get();
} on NetworkException catch (error) {
print('Network exception called, status code: ${error.code}');
}
copied to clipboard
Add middleware:
import 'package:network/interceptors.dart';

NetworkSettings().interceptors.addAll([
defaultErrors(),
Interceptor(
onRequest: (request) {
print('request on: ${request.url}');
return request.copyWith(
url: request.url + '/additional-link',
);
},
onResponse: (response) {
print('response status code: ${response.statusCode}');
return response;
},
onError: (error) {
if (error is UnauthorizedException) {
signOut();
}

return error;
},
)
]);
// Also can add middleware specify for client:
final client = Network()..interceptors.add(...);
client.get(...) // Usages global (NetworkSettings().interceptors) and local (client) interceptors
copied to clipboard
And... all api docs available here
Credits #
This software uses the following open source packages:

http

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.