https://github.com/dickermoshe/dart_mappable_extras
Untagged Unions and Undefined Types for dart_mappable
https://github.com/dickermoshe/dart_mappable_extras
dart flutter json
Last synced: 9 months ago
JSON representation
Untagged Unions and Undefined Types for dart_mappable
- Host: GitHub
- URL: https://github.com/dickermoshe/dart_mappable_extras
- Owner: dickermoshe
- License: mit
- Created: 2025-09-04T14:05:47.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-09-07T22:29:52.000Z (10 months ago)
- Last Synced: 2025-09-29T21:07:53.811Z (9 months ago)
- Topics: dart, flutter, json
- Language: Dart
- Homepage:
- Size: 32.2 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
A small library with some extras for dart_mappable.
# Untagged Unions
This library allows you to use a union type with dart_mappable.
```dart
@MappableClass(includeCustomMappers: unionMappers)
class UnionTest2 with UnionTest2Mappable {
final Union2 union;
UnionTest2({required this.union});
}
void main() {
final unionTest = UnionTest2(union: Union2.in1('test'));
final json = unionTest.toMap();
expect(json, {'union': 'test'});
final unionTest2 = UnionTest2(union: Union2.in2(1));
final json2 = unionTest2.toMap();
expect(json2, {'union': 1});
}
```
When deserializing, the mapper will attempt to deserialize the value with each mapper in order until one succeeds. If no mapper succeeds, an Exception is thrown.
# Optional
This library allows you to use an optional type with dart_mappable.
```dart
@MappableClass(includeCustomMappers: [OptionalMapper()], hook: RemoveUndefinedFields(["optional", "optionalNullable"]))
class OptionalTest with OptionalTestMappable {
final Optional optional;
final Optional optionalNullable;
OptionalTest({required this.optional, required this.optionalNullable});
}
void main() {
final optionalTest = OptionalTest(optional: Optional.defined('test'), optionalNullable: Optional.undefined());
final json = optionalTest.toMap();
expect(json, {'optional': 'test'});
}
```