api_utils 0.0.1 copy "api_utils: ^0.0.1" to clipboard
api_utils: ^0.0.1 copied to clipboard

outdated

Simple api utilities to make working with REST easier

Api Utils for Dart & Flutter #

ain't no REST for the wicked

Intro #

A collection of helper functions and an ApiResponse class that make working with REST apis a bit easier.

Usage #

Here is an example of making a GET to an endpoint that returns a list.

All you need is a model with a fromJson contructor. This ensures you app only interacts with strongly typed models, not raw JSON lists and maps.

var response = await getList(
    url: 'https://jsonplaceholder.typicode.com/posts',
    fromJson: (x) => Post.fromJson(x),
);

Similarly for POST

var newPost = Post(title: 'title', body: 'body');
var response = await post(
    url: 'https://jsonplaceholder.typicode.com/posts',
    body: newPost.toJson(),
);

Error Logging #

It can be helpful to have a single place to log errors to the console or to a logging service.

message will always be populated. exception and stack will be populated if an exception is thrown when making the request.

ApiLogger.onErrorMiddleware.add((message, exception, stack) {
    if (kReleaseMode) {
        // log to logging service
    } else {
        // log to console
    }
});
4
likes
30
points
49
downloads

Publisher

verified publisherhomex.com

Weekly Downloads

Simple api utilities to make working with REST easier

Repository (GitHub)

License

MIT (license)

Dependencies

flutter, http, meta

More

Packages that depend on api_utils