Skip to content

Instantly share code, notes, and snippets.

@PiN73
Created April 12, 2020 10:26
Show Gist options
  • Save PiN73/e03d8f73c863949dae64c7a21b139442 to your computer and use it in GitHub Desktop.
Save PiN73/e03d8f73c863949dae64c7a21b139442 to your computer and use it in GitHub Desktop.
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