An open API service indexing awesome lists of open source software.

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

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'});
}
```