Created
January 25, 2022 10:20
-
-
Save jaripekkala/dd04ac7d090ad91c4463c5b48c885291 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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