duckma_client

An interface with an adapter layer which tries to consume Dio with efficiency and smartly.

Github stars Pub Version

Installation

  1. Add this to your packages pubspec.yaml file:
dependencies:
  duckma_client: <^last>
  1. Install it You can install it from the command line:
$ flutter pub get
  1. Import it Now in Dart code, you can use:
import 'package:duckma_client/duckma_client.dart';

Usage

  1. create a HttpClient instance
  2. create a Dio instance and pass it to HttpClient
  3. Enjoy!

See the example for demonstration usage.

Get request

Nothing change.

Future<void> getAllUsers() async {
  final Response<List<dynamic>> response = await _client.get(
    Uri(path: '/users'),
  );

  final List<UserModel>? model =
  response.data?.map((e) => UserModel.fromJson(e)).toList();
  if (model != null) {
    _users.addAll(model);
  }
}

See nothing changes, but we have add something cool like the following one:

Future<void> getAllUsers() async {
  final Response<List<dynamic>> response = await _client.get(
    Uri(path: '/users'),
  );

  final List<UserModel> model =
  response.body.map((e) => UserModel.fromJson(e)).toList();
  _users.addAll(model);
}

Here we go, by using body, which came from an extension on Response, We are no more forced to check nullability in every single method, we do this once for all.

response.data = []; // no error, but its a huge problem resetting this property.
response.body = []; // compile error, There isn’t a setter named 'body' in class Response.

And this is only the beginning.

Libraries

duckma_client