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
- Host: GitHub
- URL: https://github.com/dammak/jsonplaceholder_offline
- Owner: DAMMAK
- License: mit
- Created: 2020-01-23T14:47:31.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-20T19:39:35.000Z (about 6 years ago)
- Last Synced: 2025-03-23T01:36:24.037Z (about 1 year ago)
- Topics: dart, dart-package, flutter, flutter-package
- Language: Dart
- Homepage:
- Size: 405 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
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/