Files
GitJournal/lib/note.dart
Vishesh Handa fdf8c06c24 Add Note serializers
This way we don't only need to use json.
2018-06-01 18:57:53 +02:00

70 lines
1.4 KiB
Dart

typedef NoteAdder(Note note);
typedef NoteRemover(Note note);
typedef NoteUpdator(Note note);
class Note implements Comparable {
String id;
DateTime createdAt;
String body;
Note({this.createdAt, this.body, this.id});
factory Note.fromJson(Map<String, dynamic> json) {
String id;
if (json.containsKey("id")) {
var val = json["id"];
if (val.runtimeType == String) {
id = val;
} else {
id = val.toString();
}
}
return new Note(
id: id,
createdAt: DateTime.parse(json['createdAt']),
body: json['body'],
);
}
Map<String, dynamic> toJson() {
return {
"createdAt": createdAt.toIso8601String(),
"body": body,
"id": id,
};
}
@override
int get hashCode => id.hashCode ^ createdAt.hashCode ^ body.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Note &&
runtimeType == other.runtimeType &&
id == other.id &&
body == other.body &&
createdAt == other.createdAt;
@override
String toString() {
return 'Note{id: $id, body: $body, createdAt: $createdAt}';
}
@override
int compareTo(other) => createdAt.compareTo(other.createdAt);
}
class AppState {
bool isLoading;
List<Note> notes;
AppState({
this.isLoading = false,
this.notes = const [],
});
factory AppState.loading() => AppState(isLoading: true);
}