Skip to content

Commit 25a4e44

Browse files
authored
Merge pull request #11 from DAMMAK/add-new-feature
Add new feature
2 parents c38a95b + d2acd4e commit 25a4e44

File tree

5 files changed

+65
-12
lines changed

5 files changed

+65
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
- Add object to json feature
88

99
## [1.0.1]
10+
- Update the documentation
11+
- Add get PostComments feature
12+
- Add get UserPosts feature

README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# jsonplaceholder_offline
22

3-
A simple JsonPlaceholder https://jsonplaceholder.typicode.com/ offline package that allows you to get data for your flutter project on the go without relying on the network
3+
A simple package that allows you to get data for your project on the go without relying on the network
44
## Installation
55
Add this to your package's pubspec.yaml file
66

77
```dart
88
dependencies:
9-
jsonplaceholder_offline:0.0.1
9+
jsonplaceholder_offline: 1.0.1
1010
```
1111
and run
1212
```dart
1313
flutter packages get
1414
```
1515
## How it work
16-
The package is offline version of https://jsonplaceholder.typicode.com/ JsonPlaceholder.
16+
The package is offline version of[JsonPlaceholder](https://jsonplaceholder.typicode.com)
1717
Jsonplaceholder has 6 different data categories:
1818
- User
1919
- Post
@@ -34,7 +34,7 @@ you can get data in two format either object or json.
3434
# Usage
3535

3636
## Getting Object data
37-
### list of Users
37+
### list of Users
3838
```dart
3939
JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
4040
List<User> users = jsonPlaceholder.getData<User>(length: 20);
@@ -47,14 +47,36 @@ you can get data in two format either object or json.
4747
```
4848

4949
## Getting Json data
50-
### list of users data
50+
### list of users data
5151
```dart
5252
JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
53-
List<Map<String,dynamic>> users = jsonPlaceholder.getJsonData<User>(length: 20);
53+
List<Map<String, dynamic>> users = jsonPlaceholder.getJsonData<User>(length: 20);
5454
```
5555
### a single User json
5656

5757
```dart
5858
JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
59-
Map<String,dynamic> user = jsonPlaceholder.getJsonData<User>(length:1);
60-
```
59+
Map<String, dynamic> user = jsonPlaceholder.getJsonData<User>(length:1);
60+
```
61+
### Get UserPost by userId
62+
63+
```dart
64+
JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
65+
List<dynamic> comments =
66+
jsonPlaceholder.getUserPosts(userId: 1, toJson: false);
67+
```
68+
***NB:** `toJson` property can be set to true if you want the return data to be in JSON format*
69+
70+
### Get PostComment by postId
71+
72+
```dart
73+
JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
74+
List<dynamic> comments =
75+
jsonPlaceholder.getUserPosts(postId: 1, toJson: false);
76+
```
77+
***NB:** `toJson` property can be set to true if you want the return data to be in JSON format*
78+
79+
80+
81+
82+
**Data credit:** https://jsonplaceholder.typicode.com/

lib/exception/placeholder_exception.dart

Lines changed: 0 additions & 3 deletions
This file was deleted.

lib/jsonplaceholder_offline.dart

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
library jsonplaceholder_offline;
22

33
import 'dart:math';
4+
import 'package:flutter/material.dart';
45
import 'package:jsonplaceholder_offline/models/album.dart';
56
import 'package:jsonplaceholder_offline/models/comment.dart';
67
import 'package:jsonplaceholder_offline/models/data.dart';
@@ -17,7 +18,7 @@ class JsonPlaceholder {
1718
data = loadData();
1819
}
1920

20-
dynamic getData<T>({int length}) {
21+
dynamic getData<T>({@required int length, toJson = false}) {
2122
List<dynamic> dataComments = data['comments'];
2223
List<dynamic> dataAlbums = data['albums'];
2324
List<dynamic> dataUsers = data['users'];
@@ -220,4 +221,26 @@ class JsonPlaceholder {
220221
throw ("Invalid object type");
221222
}
222223
}
224+
225+
List<dynamic> getPostComments({@required int postId, bool toJson = false}) {
226+
if (postId == null || postId == 0) return throw ("provide a valid postId");
227+
List<dynamic> comments = List<dynamic>();
228+
List<dynamic> dataComments = data['comments'];
229+
dataComments.forEach((_comment) {
230+
if (_comment["postId"] == postId)
231+
comments.add(toJson == false ? Comment.fromJson(_comment) : _comment);
232+
});
233+
return comments;
234+
}
235+
236+
List<dynamic> getUserPosts({@required int userId, bool toJson = false}) {
237+
if (userId == null || userId == 0) return throw ("provide a valid UserId");
238+
List<dynamic> posts = List<dynamic>();
239+
List<dynamic> dataPosts = data['posts'];
240+
dataPosts.forEach((_post) {
241+
if (_post["userId"] == userId)
242+
posts.add(toJson == false ? Post.fromJson(_post) : _post);
243+
});
244+
return posts;
245+
}
223246
}

test/jsonplaceholder_offline_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter_test/flutter_test.dart';
22

33
import 'package:jsonplaceholder_offline/jsonplaceholder_offline.dart';
4+
import 'package:jsonplaceholder_offline/models/comment.dart';
45
import 'package:jsonplaceholder_offline/models/photo.dart';
56
import 'package:jsonplaceholder_offline/models/post.dart';
67
import 'package:jsonplaceholder_offline/models/todo.dart';
@@ -14,6 +15,13 @@ void main() {
1415
jsonPlaceholder.getJsonData<Photo>(length: 20);
1516
expect(users.length, 20);
1617
});
18+
19+
test('testing PostComment', () {
20+
JsonPlaceholder jsonPlaceholder = JsonPlaceholder();
21+
List<dynamic> comments =
22+
jsonPlaceholder.getUserPosts(userId: 1, toJson: true);
23+
print(comments);
24+
});
1725
});
1826
}
1927

0 commit comments

Comments
 (0)