network 0.10.0+1

Dart native js
Flutter Android iOS web

Package including hooks for easy works with http package in dart

Logo

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

Pub

Key FeaturesGetting StartedTodoCredits

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();

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);

Get request Blob:

final blobResponse = await 'https://via.placeholder.com/300'.get();
print(blobResponse.bytes);

Post request to API:

final postResponse = await 'https://jsonplaceholder.typicode.com/todos'.post(
  body: {'title': 'test'},
);
print(postResponse.asMap['id']);

Handle exceptions:

try {
  await 'https://jsonplaceholder.typicode.com/todos/202'.get();
} on NetworkException catch (error) {
  print('Network exception called, status code: ${error.code}');
}

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

And... all api docs available here

Credits #

This software uses the following open source packages:

6
likes
90
pub points
72%
popularity

Publisher

serge.software

Package including hooks for easy works with http package in dart

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

http, meta

More

Packages that depend on network