https://github.com/hoc081098/json_list_generic
Demo how to parse `JSON` to a generic list `List<T>` with `T` is the model class you defined (use `json_annotation` library).
https://github.com/hoc081098/json_list_generic
Last synced: 7 months ago
JSON representation
Demo how to parse `JSON` to a generic list `List<T>` with `T` is the model class you defined (use `json_annotation` library).
- Host: GitHub
- URL: https://github.com/hoc081098/json_list_generic
- Owner: hoc081098
- License: mit
- Created: 2021-07-19T08:37:10.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T02:32:55.000Z (almost 2 years ago)
- Last Synced: 2025-01-30T23:36:54.681Z (9 months ago)
- Language: Dart
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# json_list_generic
Demo how to parse `JSON` to a generic list `List` with `T` is the model class you defined (use `json_annotation` library).
# Run example: `bin/main.dart`
```dart
void main(List arguments) {
Factories.shared.registerFromJson((json) => User.fromJson(json as dynamic));
Factories.shared.registerToJson((user) => user.toJson());Factories.shared.registerFromJson((json) => Item.fromJson(json as dynamic));
final jsonString1 = jsonEncode({
'page': 1,
'items': [
User('1', 'name 1'),
User('2', 'name 2'),
User('3', 'name 3'),
User('4', 'name 4'),
],
});
var users = PaginationResult.fromJson(jsonDecode(jsonString1));
print(users);
print(users.toJson());print('\n------------------------\n');
final jsonString2 = jsonEncode({
'page': 1,
'items': [
Item('1', 'name 1'),
Item('2', 'name 2'),
Item('3', 'name 3'),
Item('4', 'name 4'),
],
});
var items = PaginationResult.fromJson(jsonDecode(jsonString2));
print(items);
print(items.toJson());
}
```