jsonplaceholder_offline 1.0.3 copy "jsonplaceholder_offline: ^1.0.3" to clipboard
jsonplaceholder_offline: ^1.0.3 copied to clipboard

A simple JsonPlaceholder offline package that allows you to get data for your flutter project on the go without relying on the network

jsonplaceholder_offline #

A simple package that allows you to get data for your project on the go without relying on the network

Installation #

Add this to your package's pubspec.yaml file

dependencies:
    jsonplaceholder_offline: 1.0.3

and run

flutter packages get

How it work #

The package is offline version ofJsonPlaceholder Jsonplaceholder has 6 different data categories:

  • User
  • Post
  • Comment
  • Album
  • Photo
  • Todo

you can get data in two format either object or json.

  • to get object data you will need to call getData<T>(length:1) method with length as a parameter
  • to get json data you will need to call getJsonData<T>(length:1) method with length as a parameter

NB:

  • length is number of data you wish to return. from (min 1 - max 100)

  • T is the generic reference of your category.

    Usage #

    Getting Object data #

list of Users #

JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
List<User> users = jsonPlaceholder.getData<User>(length: 20);

a single User object #

JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
User user = jsonPlaceholder.getData<User>(length:1);

Getting Json data #

list of users data #

JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
List<Map<String, dynamic>> users = jsonPlaceholder.getJsonData<User>(length: 20);

a single User json #

JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
Map<String, dynamic> user = jsonPlaceholder.getJsonData<User>(length:1);

Get UserPost by userId #

 JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
 List<dynamic> comments = jsonPlaceholder.getUserPosts(userId: 1, toJson: false);

*NB: toJson property can be set to true if you want the return data to be in JSON format*

Get PostComment by postId #

JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
List<dynamic> comments = jsonPlaceholder.getUserPosts(postId: 1, toJson: false);

*NB: toJson property can be set to true if you want the return data to be in JSON format*

Mocking HTTP Request #

if you want to get your data in HTTP Request behaviour, you can mock the behaviour using mockHttp<T>({RESTMETHOD httpMethod, int delay, int length})

JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
Map<String, dynamic> data = await jsonPlaceholder.mockHttp<User>(httpMethod: RESTMETHOD.GET, length: 10, delay: 20);

NB:

  • httpMethod: {GET,POST,PUT,DELETE}
  • delay: This is the number of seconds you want your request to take before it return data.
  • length: This is the number of data you want to return

Data credit: https://jsonplaceholder.typicode.com/

5
likes
40
pub points
0%
popularity

Publisher

verified publisherdammak.dev

A simple JsonPlaceholder offline package that allows you to get data for your flutter project on the go without relying on the network

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on jsonplaceholder_offline