Logo

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

Pub

Key FeaturesGetting StartedTodoCredits

Key Features

  • Simple hooks for get / post
  • Specify response type (blob\json api)

Getting Started

// Simplify usage
import 'package:network/hooks.dart' as network;
network.get(...);
network.post(...);

// Alternative usage
import 'package:network/network.dart';
final client = Network();
client.get(...);
client.post(...);
client.close();

For new users (0.8.0+) before all usages need disable backward compatibility

network.settings.disableLegacy();

Get request to API:

final restResponse = await network.get<network.JsonApiResponse>(
  'https://jsonplaceholder.typicode.com/todos/1',
  queryParameters: {'name': 'value'}
);
print(restResponse.toMap['title']);

Get request Blob:

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

Add middleware:

import 'package:network/middlewares.dart';

network.settings.middleware.addAll([
  defaultErrors(),
  network.Middleware(
    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;
    },
  )
]);

And... all api docs available here

Credits

This software uses the following open source packages:

Libraries

hooks
middlewares
network