Add base model

This commit is contained in:
2022-10-28 10:33:56 +07:00
parent 3b2abdc030
commit a472156d22
3 changed files with 60 additions and 10 deletions

3
lib/model.dart Normal file
View File

@@ -0,0 +1,3 @@
library model;
export 'src/model/base.dart';

33
lib/src/model/base.dart Normal file
View File

@@ -0,0 +1,33 @@
import 'dart:convert';
abstract class BaseModel<T> {
BaseModel();
Map<String, dynamic> toMap();
T fromMap(Map<String, dynamic> map);
T fromJson(String json) {
return fromMap(decode(json));
}
@override
String toString() {
return toMap().toString();
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is BaseModel && other.toMap() == toMap();
}
@override
int get hashCode => toMap().hashCode;
String toJson() => encode(toMap());
dynamic decode(String json) => jsonDecode(json);
String encode(dynamic json) => jsonEncode(json);
}