https://github.com/hoc081098/built_value_built_list_json_sample
https://github.com/hoc081098/built_value_built_list_json_sample
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/hoc081098/built_value_built_list_json_sample
- Owner: hoc081098
- Created: 2021-07-27T18:21:50.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-10-20T05:43:53.000Z (almost 2 years ago)
- Last Synced: 2023-10-20T14:20:31.327Z (almost 2 years ago)
- Language: Dart
- Size: 29.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# built_value_built_list_json_sample
`built_value` sample: parse `JSON` to `BuiltList` dynamically.## Author: [Petrus Nguyễn Thái Học](https://github.com/hoc081098)
[](https://github.com/hoc081098/test_built_value/actions/workflows/dart.yml)
[](https://pub.dev/packages/lints)## Use
### Step 1. Define your `Built` classes in `_types`
```dart
/// Register your [Built] classes here.
const List _types = [
MyModel,
MyModelTwo,
];
```### Step 2. Define your `BuilderFactory`s in `_factories`.
```dart
/// Register your [_BuilderFactory]s here.
final List<_BuilderFactory> _factories = [
_BuilderFactory(
FullType(BuiltMap, [
FullType(String),
FullType(int),
]),
() => MapBuilder(),
),
];
```### Step 3. Use `deserializeBuiltList` and `deserializeBuiltListNullable` to parse `JSON` to `BuiltList`.
```dart
abstract class MyModel implements Built {
BuiltList get items;MyModel._();
factory MyModel([void Function(MyModelBuilder) updates]) = _$MyModel;
[...]
}
```#### `BuiltList deserializeBuiltList(Object json)`
```dart
final json = [
{
'items': [1, 2, null, 3, 3]
},
{
'items': [1, 2, null, 3, 2]
},
{
'items': [1, 2, null, 3]
},
];BuiltList items = deserializeBuiltList(json);
```#### `BuiltList deserializeBuiltListNullable(Object json)`.
```dart
final json = [
{
'items': [1, 2, null, 3, 3]
},
{
'items': [1, 2, null, 3, 2]
},
null,
{
'items': [1, 2, null, 3]
},
];BuiltList items = deserializeBuiltListNullable(json);
```## Run demo
```shell
dart pub get
dart run ./bin/main.dart
```