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

Convenient api utilities to make working with REST APIs easier

Api Utils for Dart & Flutter #

pub package

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. Json_serializable is a commonly used dart package that provides this functionality.

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

Publish / Release New Version #

4
likes
100
pub points
50%
popularity

Publisher

verified publisherhomex.com

Convenient api utilities to make working with REST APIs easier

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, http

More

Packages that depend on api_utils