Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
/ api_utils Public archive

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

License

Notifications You must be signed in to change notification settings

HomeXLabs/api_utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •  

Languages