Created
April 12, 2020 10:26
-
-
Save PiN73/e03d8f73c863949dae64c7a21b139442 to your computer and use it in GitHub Desktop.
This file contains 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
import 'package:flutter/foundation.dart'; | |
extension NumExtension on num { | |
bool greaterThan(num other) => this > other; | |
} | |
class MyClass { | |
final bool a; | |
final bool b; | |
final bool c; | |
MyClass({ | |
@required this.a, | |
@required this.b, | |
@required this.c, | |
}); | |
static MyClass fromJson(Map<String, Object> map) { | |
return MyClass( | |
a: (map['a'] as int)?.greaterThan(0), | |
b: (map['b'] as int)?.greaterThan(0), | |
c: (map['c'] as int)?.greaterThan(0), | |
); | |
} | |
@override | |
String toString() => 'a=$a b=$b c=$c'; | |
} | |
void main() { | |
final map = { | |
'a': 0, | |
'b': 1, | |
'c': null, | |
}; | |
final obj = MyClass.fromJson(map); | |
print(obj); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment