Add base model
This commit is contained in:
3
lib/model.dart
Normal file
3
lib/model.dart
Normal file
@@ -0,0 +1,3 @@
|
||||
library model;
|
||||
|
||||
export 'src/model/base.dart';
|
||||
33
lib/src/model/base.dart
Normal file
33
lib/src/model/base.dart
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user