Skip to content

Instantly share code, notes, and snippets.

@jaripekkala
Created January 25, 2022 10:20
Show Gist options
  • Save jaripekkala/dd04ac7d090ad91c4463c5b48c885291 to your computer and use it in GitHub Desktop.
Save jaripekkala/dd04ac7d090ad91c4463c5b48c885291 to your computer and use it in GitHub Desktop.
enum Alignment { center }
// Domain model
class Image {
final int width;
final Alignment alignment;
Image({required this.width, required this.alignment});
}
// JSON model
// 1. lazy deserialization
// 2. implements domain model, no need for converters
// 3. contains the JSON map, no need for serialization
//
// TODO memoisation to prevent double parsing
class ImageJsonView implements Image {
final Map json;
@override
int get width => json['width'] ?? (throw 'parse error');
@override
Alignment get alignment =>
json['align'] == 'center' ? Alignment.center : (throw 'parse error');
ImageJsonView(this.json);
@override
String toString() => 'ImageJsonView$json';
}
void main() {
final Image image = ImageJsonView({
"width": 100,
"align": "center",
});
print(image);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment