duckma_client
An interface with an adapter layer which tries to consume Dio with efficiency and smartly.
Installation
- Add this to your packages pubspec.yaml file:
dependencies:
duckma_client: <^last>
- Install it You can install it from the command line:
$ flutter pub get
- Import it Now in Dart code, you can use:
import 'package:duckma_client/duckma_client.dart';
Usage
- create a HttpClient instance
- create a Dio instance and pass it to HttpClient
- 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.