An open API service indexing awesome lists of open source software.

https://github.com/dammak/jsonplaceholder_offline

A simple jsonplaceholder package that allow you to get a random data for your flutter project without relying on the internet
https://github.com/dammak/jsonplaceholder_offline

dart dart-package flutter flutter-package

Last synced: about 1 month ago
JSON representation

A simple jsonplaceholder package that allow you to get a random data for your flutter project without relying on the internet

Awesome Lists containing this project

README

          

# 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

```dart
dependencies:
jsonplaceholder_offline: 1.0.3
```

and run

```dart
flutter packages get
```

## How it work

The package is offline version of[JsonPlaceholder](https://jsonplaceholder.typicode.com)
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(length:1)` method with length as a parameter
- to get json data you will need to call `getJsonData(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

```dart
JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
List users = jsonPlaceholder.getData(length: 20);
```

### a single User object

```dart
JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
User user = jsonPlaceholder.getData(length:1);
```

## Getting Json data

### list of users data

```dart
JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
List> users = jsonPlaceholder.getJsonData(length: 20);
```

### a single User json

```dart
JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
Map user = jsonPlaceholder.getJsonData(length:1);
```

### Get UserPost by userId

```dart
JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
List 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

```dart
JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
List 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({RESTMETHOD httpMethod, int delay, int length})`

```dart
JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
Map data = await jsonPlaceholder.mockHttp(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/